All files utils.js

98.21% Statements 55/56
94.29% Branches 33/35
100% Functions 23/23
97.3% Lines 36/37
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  99x 119x   7x 65x       23x   7x 32x       28x   7x 2x   7x 23x 48x 2x 28x 55x     458x 458x 456x 388x 356x 149x 136x 122x   207x 83x     291x 335x 123x 52x       7x 108x 108x   1x 1x             7x                                  
// Argument validation
const isValidObjectWith = fields => obj =>
  !!obj && !Array.isArray(obj) && fields.every(field => obj.hasOwnProperty(field))
 
const isValidAction = obj => {
  return isValidObjectWith(['message', 'func'])(obj) &&
    typeof obj.func === 'function' && typeof obj.message === 'string'
}
 
const isValidActionsArray = arr => arr.every(isValidAction)
 
const isValidPostParams = obj => {
  return isValidObjectWith(['message', 'args'])(obj) &&
    Array.isArray(obj.args) && typeof obj.message === 'string'
}
 
const isValidPostParamsArray = arr => arr.every(isValidPostParams)
 
const isValidObjectsArray = arr => (fields = []) =>
  arr.every(isValidObjectWith(fields))
 
const testArray = {
  'actionsArray': arr => isValidActionsArray(arr),
  'arraysArray': arr => arr.every(item => Array.isArray(item)),
  'objectsArray': arr => isValidObjectsArray(arr)(),
  'postParamsArray': arr => isValidPostParamsArray(arr),
  'stringsArray': arr => arr.every(item => typeof item === 'string')
}
 
const isValidArg = arg => type => {
  if (type === 'null') return arg === null
  if (type === 'undefined') return arg === undefined
  if (type === 'action') return isValidAction(arg)
  if (Array.isArray(arg)) {
    if (type !== 'array' && !testArray[type]) return false
    if (type === 'array') return true
    return testArray[type](arg)
  }
  if (arg) return typeof arg === type.toString() // eslint-disable-line
  return false
}
 
const isValid = argument => (types = null) => {
  if (Array.isArray(types)) return types.some(type => isValidArg(argument)(type))
  if (isValidArg(argument)(types)) return true
  return false
}
 
// Argument error builder
const argumentError = ({ expected = '', received, extraInfo = '' }) => {
  try {
    return new TypeError(`${'You should provide ' + expected}${'\n' + extraInfo}${'\nReceived: ' + JSON.stringify(received)}`)
  } catch (err) {
    Eif (err.message === 'Converting circular structure to JSON') {
      return new TypeError(`${'You should provide ' + expected}${'\n' + extraInfo}${'\nReceived a circular structure: ' + received}`)
    }
    throw err
  }
}
 
// Response builder
const makeResponse = work => `
  self.onmessage = event => {
    const args = event.data.message.args
    if (args) {
      self.postMessage((${work}).apply(null, args))
      return close()
    }
    self.postMessage((${work})())
    return close()
  }
`
 
export {
  makeResponse,
  argumentError,
  isValid
}