All files / src actions.js

97.67% Statements 42/43
100% Branches 2/2
95.83% Functions 23/24
97.3% Lines 36/37

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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80  1x 1x   1x   1x   40x 280x 100x   1x 20x 20x   80x 80x   20x     1x 20x   93x 93x     91x           1x     24x     11x     18x     12x     2x     93x     2x         20x     2x       1x 1x   1x 1x 1x 1x   1x 1x    
 
import forceArray from 'force-array'
import fl from 'fantasy-land'
 
import { isString } from './utils'
 
const rFANTASY = /^fantasy-land\//
 
const alias = c => Object.getOwnPropertyNames(c)
  .filter(n => rFANTASY.test(n))
  .map(n => ([n.replace(rFANTASY, ''), c[n]]))
 
const aliasType = c => {
  alias(c).forEach(([alias, fn]) => {
    c[alias] = fn
  })
  alias(c.prototype).forEach(([alias, fn]) => {
    c.prototype[alias] = fn
  })
  return c
}
 
export const createAction = name => {
  var action = { [name]: class {
    constructor (value) {
      this.__value = value
      this['@@type'] = name
    }
    static [fl.of] (value) {
      return new action[name](value)
    }
    static of (value) {
      return new action[name](value)
    }
    static is (type) {
      return type instanceof action[name]
    }
    [fl.map] (fn) {
      return action[name].of(fn(this.join()))
    }
    [fl.ap] (m) {
      return m.chain(fn => this.map(fn))
    }
    [fl.chain] (fn) {
      return fn(this.join())
    }
    [fl.equals] (a) {
      return a.join() === this.join()
    }
    lift (m) {
      return m.map(this.__value)
    }
    join () {
      return this.__value
    }
    unwrapOrElse (_default) {
      return typeof this.join() === 'undefined'
        ? _default
        : this.join()
    }
  } }
  return aliasType(action[name])
}
 
export const createActions = actions => forceArray(actions)
  .filter(isString)
  .map(createAction)
 
export const connect = signal => actions =>
  createActions(actions)
    .map(Action => {
      Action.of = value => {
        const action = new Action(value)
        signal.emit(action)
        return action
      }
      Action[fl.of] = Action.of
      return Action
    })