All files postAll.js

100% Statements 20/20
100% Branches 15/15
100% Functions 9/9
100% Lines 12/12
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      2x 30x                 38x 10x   10x 3x     8x 3x     14x 12x       30x 30x    
// import { invalidObjectsArray, isArrayOf, notArray, returnNull, wrongLength, wrongObjects } from './utils'
import { isValid, argumentError } from './utils'
 
const makeOptionsFor = arr => {
  return {
    expected: 'an array of arrays, an array of objects, or an array of strings',
    received: arr,
    extraInfo: 'If an array of arrays, ' +
      'it must have the same length as the actions registered for this worker.\n' +
      'If an array of objects, every object must containing two fields:\n* message\n* args'
  }
}
export function postAll (arr = []) {
  if (isValid(arr)(['arraysArray', 'postParamsArray', 'stringsArray'])) {
    if (arr.length === 0) return Promise.all(this.actions.map(({ message }) => this.postMessage(message)))
 
    if (arr.every(item => typeof item === 'string')) {
      return Promise.all(arr.map(msg => this.postMessage(msg)))
    }
 
    if (arr.every(item => typeof item === 'object' && !Array.isArray(item))) {
      return Promise.all(arr.map(({ message, args }) => this.postMessage(message, args)))
    }
 
    if (arr.every(item => Array.isArray(item)) && arr.length === this.actions.length) {
      return Promise.all(arr.map((args, index) => this.postMessage(this.actions[index].message, args)))
    }
  }
 
  console.error(argumentError(makeOptionsFor(arr)))
  return null
}