All files / src transform.js

100% Statements 24/24
93.33% Branches 14/15
100% Functions 6/6
100% Lines 20/20

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 38 39 40 41 42 43 44 45 46 47  1x   1x 1x           5x 5x 6x 1x     5x 5x   5x 1x     4x 4x   6x             1x 3x 3x 2x     1x         3x    
 
import { createAction } from './actions'
 
const typeId = '@@type'
const globalStore = {}
 
/**
 * Converts regular events in to typed events
 * Can be used with update functions that expect type events
 */
export const typeEvent = (update, store = globalStore, create = createAction) => {
  const map = event => {
    if (!event.type) {
      return event
    }
 
    const { type, payload = null } = event
    const typed = store[type]
 
    if (typed) {
      return typed.of(payload)
    }
 
    store[type] = create(type)
    return store[type].of(payload)
  }
  return (state, event) => update(state, map(event))
}
 
/**
 * Converts typed events in to regular events
 * Can be used with update functions that expect regular events
 */
export const untypeEvent = update => {
  const map = event => {
    if (!event.join || !event[typeId]) {
      return event
    }
 
    return {
      type: event[typeId],
      payload: event.join()
    }
  }
  return (state, event) => update(state, map(event))
}