All files / src patch.js

100% Statements 17/17
90% Branches 9/10
100% Functions 7/7
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      1x 6x 1x         5x       9x           2x     1x 9x 1x     8x 2x     6x     1x  
 
// Will usually provide back the state object, but, if this is an arc
// being patched then it will need to pass back a function
const generateState = (state, key) => {
  if (typeof state === 'function') {
    return selector => selector
      ? selector(state()[key])
      : state()[key]
  }
 
  return state[key]
}
 
// Provides selected key to update and applies back to state
const select = (key, update) => (state, event) => ({
  ...state,
  [key]: update(generateState(state, key), event)
})
 
// Returns a function accepting an update
const createPatch = key => update => select(key, update)
 
// Allow fixed arity for easier composition
const patch = (key, update) => {
  if (!key) {
    throw new Error('[raid:patch] key must be supplied')
  }
 
  if (!update || typeof update !== 'function') {
    return createPatch(key)
  }
 
  return select(key, update)
}
 
export default patch