Skip to content

Latest commit

 

History

History
386 lines (254 loc) · 6.93 KB

API.md

File metadata and controls

386 lines (254 loc) · 6.93 KB

Table of Contents

match

Syntax

match(predicateUpdater, leftUpdater)
match(predicateUpdater, leftUpdater, rightUpdater)
match(predicateUpdater)(leftUpdater)
match(predicateUpdater)(leftUpdater, rightUpdater)

Description

Creates a proxy updater that:

  • calls leftUpdater if predicateUpdater returns true, or
  • calls rightUpdater if predicateUpdater returns false and rightUpdater is defined, or
  • returns the state unchanged if predicateUpdater returns false and rightUpdater is undefined.

Parameters

  • predicateUpdater function (action: any): function (state: any): boolean
  • leftUpdater Updater
  • rightUpdater Updater?

Examples

match(p, t, f)
// Is equivalent to
action => state => p(action)(state) ? t(action)(state) : f(action)(state)
match(p, t)
// Is equivalent to
action => state => p(action)(state) ? t(action)(state) : state

Returns Updater

withDefaultState

Syntax

withDefaultState(defaultState, updater)
withDefaultState(defaultState)(updater)

Description

Creates a proxy updater that:

calls updater with defaultState if incoming state is undefined, or calls it with the incoming state.

Parameters

Examples

withDefaultState(0, add)
// Is equivalent to
action => (state = 0) => add(action)(state)

Returns Updater

concat

Syntax

concat(...updaters)

Description

Creates a proxy updater that:

calls each updater with the preceding updater's outgoing state (left-to-right).

Parameters

Examples

concat(f, g, h)
// Is equivalent to
action => state => h(action)(g(action)(f(action)(state)))

Returns Updater

combine

Syntax

combine(pathFragmentUpdaterMap)

Description

Like combineReducers, but for updaters.

Parameters

  • pathFragmentUpdaterMap Object

Examples

combine({
  foo: fooUpdater,
  bar: barUpdater
})
// Is equivalent to
concat(
  updateStateAt('foo', fooUpdater),
  updateStateAt('bar', barUpdater)
)

Returns Updater

handleAction

Syntax

handleAction(actionType, updater)
handleAction(actionType)(updater)

Description

Creates a proxy updater that:

calls updater if actionType matches incoming action's type, or returns the state unchanged.

Parameters

Examples

handleAction('SOME_ACTION', someUpdater)
// Is equivalent to
match(action => () => action.type === 'SOME_ACTION', someUpdater)

Returns Updater

handleActions

Syntax

handleActions(actionTypeUpdaterMap)

Description

Creates a proxy updater that:

delegates to a matching updater from actionTypeUpdaterMap based on the incoming action's type, or returns the state unchanged.

Parameters

Examples

handleActions({
  ACTION_ONE: updaterOne,
  ACTION_TWO: updaterTwo
})
// Is equivalent to
concat(
  handleAction('ACTION_ONE', updaterOne),
  handleAction('ACTION_TWO', updaterTwo)
)

Returns Updater

updateStateAt

Syntax

updateStateAt(path, updater)
updateStateAt(path)(updater)

Description

Creates a proxy updater that:

calls updater with incoming state focused at path, then merges the result into outgoing state.

Parameters

Examples

const updater = updateStateAt('a.b', () => state => state + 1)
const state = { a: { b: 1 }, c: 0 }

updater()(state)
// Result: `{ a: { b: 2 }, c: 0 }`

Returns Updater

mapState

Syntax

mapState(updater)

Description

Creates a proxy updater that:

maps incoming state via updater as iteratee.

Parameters

Examples

mapState(action => state => state + action)
// Is equivalent to
action => state => state.map(item => item + action)

Returns Updater

filterState

Syntax

filterState(updater)

Description

Creates a proxy updater that:

filters incoming state via updater as predicate.

Parameters

  • updater function (action: any): function (state: any): boolean

Examples

filterState(action => state => action > state)
// Is equivalent to
action => state => state.filter(item => action > item)

Returns Updater

constantState

Syntax

constantState(value)

Description

Creates an updater that always returns value.

Parameters

  • value any

Examples

constantState('foo')
// Is equivalent to
() => () => 'foo'

Returns Updater

decorate

Syntax

decorate(...fns, value)

Description

Applies to value a function created by composing ...fns.

Parameters

Examples

decorate(f, g, h, value)
// Is equivalent to
f(g(h(value)))
decorate(
  withDefaultState(0),
  handleAction('ADD'),
  action => state => state + action.payload
)
// Is equivalent to
withDefaultState(0, handleAction('ADD', action => state => state + action.payload))

Returns any

Updater

Action-first curried reducer.

Type: function (action: any): function (state: any): any