All files register.js

100% Statements 24/24
100% Branches 7/7
100% Functions 10/10
100% Lines 20/20
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    24x 32x   2x 10x   24x 24x 10x 10x   14x     2x 10x             28x 24x 14x 8x 18x 18x       6x   10x 10x    
import { argumentError, isValid } from './utils'
 
const isActionOf = actions => newAction =>
  actions.some(action => action.message === newAction.message)
 
const warnMsg = action =>
  `WARN! An action with message "${action.message}" is already registered for this worker`
 
const pushInto = actions => action => {
  if (isActionOf(actions)(action)) {
    console.warn(warnMsg(action))
    return actions.length
  }
  return actions.push(action)
}
 
const makeOptionsFor = action => {
  return {
    expected: 'an array of actions or an action',
    received: action,
    extraInfo: 'Every action should be an object containing two fields:\n* message\n* func'
  }
}
 
export const register = actions => (action = null) => {
  if (isValid(action)(['action', 'actionsArray'])) {
    if (Array.isArray(action)) {
      return action.reduce((actions, action) => {
        pushInto(actions)(action)
        return actions
      }, actions).length
    }
 
    return pushInto(actions)(action)
  }
  console.error((argumentError(makeOptionsFor(action))))
  return null
}