All files fold.js

100% Statements 17/17
94.44% Branches 17/18
100% Functions 3/3
100% Lines 17/17
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        1x 1x 1x           1x             12x 3x 3x       9x         18x 9x 9x 1x 8x 1x   7x     18x               2x                    
/**
 * Module Dependencies
 */
 
var assign = require('object-assign')
var isObject = require('./is-object')
var isError = require('./is-error')
 
/**
 * Export `fold`
 */
 
module.exports = fold
 
/**
 * Fold the arguments into an action
 */
 
function fold (action) {
  if (action.length === 1 && isObject(action[0])) {
    action[0].payload = action[0].payload || {}
    return isError(action[0].payload)
      ? assign(action[0], error(action[0].payload))
      : action[0]
  } else {
    return action.reduce(folder, { payload: {} })
  }
}
 
function folder (action, value) {
  if (typeof value === 'string' && !action.type) {
    action.type = value
  } else if (isObject(value)) {
    action.payload = assign(action.payload || {}, value)
  } else if (isError(value)) {
    action = assign(action, error(value))
  } else {
    action.payload = value
  }
 
  return action
}
 
/**
 * Create an object from an error
 */
 
function error (err) {
  return {
    error: true,
    payload: {
      message: err.message,
      stack: err.stack,
      name: err.name,
      code: err.code
    }
  }
}