All files / common utils.js

74.4% Statements 93/125
75.86% Branches 44/58
72.22% Functions 26/36
75.7% Lines 81/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2072x     2x 2x               2x 6x 6x     2x 124x 122x 118x 116x 58x 62x   4x     2x   28x 196x 196x 110x 2x 2x 2x       4x 4x 4x 4x 2x   2x                                                                                                           2x       52x 52x 52x   98x 38x 38x   12x 12x 12x         2x 20x       2x 18x 22x 22x   18x         12x       2x 2x 2x 2x       2x 2x 2x 2x       4x       2x 2x 2x       6x       8x 8x   8x 8x 34x 6x 6x   28x 28x           2x 2x 2x       6x 6x 6x 14x 14x         6x 14x 6x       2x 2x    
((that, register) => {
  'use strict'
 
  Eif (typeof (exports) === 'object') {
    register(module.exports)
  } else {
    that.PapanUtils = {}
    register(that.PapanUtils)
  }
})(global, that => {
  'use strict'
 
  that.isElectron = () => {
    let p = typeof (process) !== 'undefined' && process
    return !!p && !!p.versions && !!p.versions.electron
  }
 
  that.areArraysEqual = (a1, a2) => {
    if (a1 === a2) return true
    if (!Array.isArray(a1)) return false
    if (!Array.isArray(a2)) return false
    if (a1.length !== a2.length) return false
    for (let i = 0; i < a1.length; i++) {
      if (a2.indexOf(a1[i]) < 0) return false
    }
    return true
  }
 
  that.JSON = {
    stringify: JSON.stringify,
    parse: string => JSON.parse(string, (key, value) => {
      Iif (value === null) return value
      if (typeof value !== 'object') return value
      if (!that.areArraysEqual(Object.keys(value), ['type', 'data'])) return value
      Iif (value.type !== 'Buffer') return value
      Iif (!Array.isArray(value.data)) return value
      return Buffer.from(value.data)
    })
  }
 
  that.delayedPromise = (time, value, pass = true) => new Promise((resolve, reject) => {
    let wait = setTimeout(() => {
      clearTimeout(wait)
      if (pass) {
        resolve(value)
      } else {
        reject(value)
      }
    }, time)
  })
 
  class Queuer {
    constructor (owner) {
      this.ons = []
      this.onces = []
      this.sends = []
      this.owner = owner
      this.onready = () => {}
    }
 
    ready () {
      return false
    }
 
    on (event, callback) {
      if (this.channel) {
        this.channel.on(event, callback)
      } else {
        this.ons.push({ event: event, callback: callback })
      }
    }
 
    once (event, callback) {
      if (this.channel) {
        this.channel.once(event, callback)
      } else {
        this.onces.push({ event: event, callback: callback })
      }
    }
 
    send (event, data = {}, metadata = {}) {
      if (this.channel) {
        this.channel.send(event, data, metadata)
      } else {
        this.sends.push({ event: event, data: data, metadata: metadata })
      }
    }
 
    spillover (channel) {
      console.log('Switching to live channel')
      this.channel = channel
      this.owner.channel = channel
      this.ons.forEach(on => channel.on(on.event, on.callback))
      this.onces.forEach(once => channel.once(once.event, once.callback))
      this.sends.forEach(send => channel.send(send.event, send.data, send.metadata))
      this.onready()
      return channel
    }
  }
 
  that.Queuer = Queuer
 
  class Path {
    constructor (path) {
      this._array = []
      this._absolute = false
      switch (typeof path) {
        case 'string':
          this._array = path.split('/').filter(f => !(['', '.'].includes(f)))
          this._absolute = path.startsWith('/')
          break
        case 'object':
          this._array = path._array.concat([])
          this._absolute = path._absolute
          break
      }
    }
 
    static _methodList () {
      return Object.getOwnPropertyNames(Path.prototype)
        .filter(name => !(['constructor', 'toString'].includes('join')) && !name.startsWith('_'))
    }
 
    static _wrappers () {
      return Path._methodList().reduce((output, name) => {
        output[name] = (path, ...args) => {
          const ret = (new Path(path))[name](...args)
          return typeof ret === 'object' ? ret.toString() : ret
        }
        return output
      }, {})
    }
 
    toString () {
      return (this._absolute ? '/' : '') + this._array.join('/')
    }
 
    basename () {
      const path = new Path()
      path._normalize()
      Eif (this._array.length !== 0) path._array.push(this._array[this._array.length - 1])
      return path
    }
 
    dirname () {
      const path = new Path(this)
      path._normalize()
      path._array.pop()
      return path
    }
 
    isAbsolute () {
      return this._absolute
    }
 
    isValid () {
      const path = new Path(this)
      path._normalize()
      return !path._absolute || path._array[0] !== '..'
    }
 
    isBelow () {
      return this._array[0] === '..'
    }
 
    _normalize () {
      let copy = this._array.concat([])
      let fragmentsCount = 0
 
      this._array = []
      for (let i = 0; i < copy.length; i++) {
        if (copy[i] === '..' && fragmentsCount !== 0) {
          fragmentsCount--
          this._array.pop()
        } else {
          if (copy[i] !== '..') fragmentsCount++
          this._array.push(copy[i])
        }
      }
    }
 
    normalize () {
      const path = new Path(this)
      path._normalize()
      return path
    }
 
    join (...args) {
      const path = new Path(this)
      const fragments = []
      for (let i = 0; i < args.length; i++) {
        Eif (typeof args[i] === 'string') {
          fragments.push(new Path(args[i]))
        } else {
          fragments.push(args[i])
        }
      }
      path._absolute = this._absolute
      path._array = this._array.concat(...fragments.map(element => element._array))
      return path
    }
  }
 
  that.path = Path._wrappers()
  that.Path = Path
})