All files unregister.js

100% Statements 18/18
100% Branches 7/7
100% Functions 7/7
100% Lines 15/15
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    16x 20x 16x     16x     2x 10x           24x 20x 10x 6x 12x 12x     4x     10x 10x    
import { argumentError, isValid } from './utils'
 
const removeFrom = actions => msg => {
  const index = actions.findIndex(({ message }) => message === msg)
  index === -1
    ? console.warn(`WARN! Impossible to unregister action with message "${msg}".\nIt is not a registered action for this worker.`)
    : actions.splice(index, 1)
  return actions
}
 
const makeOptions = msg => {
  return {
    expected: 'an array of strings or a string',
    received: msg
  }
}
 
export const unregister = actions => (msg = null) => {
  if (isValid(msg)(['string', 'stringsArray'])) {
    if (Array.isArray(msg)) {
      return msg.reduce((actions, message) => {
        removeFrom(actions)(message)
        return actions
      }, actions).length
    }
    return removeFrom(actions)(msg).length
  }
 
  console.error(argumentError(makeOptions(msg)))
  return null
}