reducer-patterns
v0.3.0
Published
A collection of helpers for building reducer functions (usable for Redux, Array().reduce, or any other reducing code)
Downloads
3
Readme
reducer-patterns
reducer-patterns : object
Kind: global namespace
- reducer-patterns : object
- .async : object
- .ConditionalReducer(condition, onTrue, onFalse) ⇒ function
- .InitState(initializer) ⇒ taskFunction
- .PostprocessState(reducer, postprocessor) ⇒ function
- .SeriesReducer(...reducers) ⇒ function
- .SwitchReducer(...reducers) ⇒ function
- .ConditionalReducer(condition, onTrue, onFalse) ⇒ function
- .InitState(initializer) ⇒ taskFunction
- .PostprocessState(reducer, postprocessor) ⇒ function
- .SeriesReducer(...reducers) ⇒ function
- .SwitchReducer(...reducers) ⇒ function
- .async : object
reducer-patterns.async : object
Kind: static namespace of reducer-patterns
- .async : object
- .ConditionalReducer(condition, onTrue, onFalse) ⇒ function
- .InitState(initializer) ⇒ taskFunction
- .PostprocessState(reducer, postprocessor) ⇒ function
- .SeriesReducer(...reducers) ⇒ function
- .SwitchReducer(...reducers) ⇒ function
async.ConditionalReducer(condition, onTrue, onFalse) ⇒ function
let ConditionalReducer = require('reducer-patterns/async/ConditionalReducer');
let state = {
a: 1,
b: 2,
};
let action1 = {
type: 'a',
payload: 2,
};
let action2 = {
type: 'b',
payload: 2,
};
let reducer = ConditionalReducer(
(state, action) => action.type === 'a',
(state, action) => ({
...state,
a: action.payload,
})
);
// prints action1 result { a: 2, b: 2 }
// action1 triggers a state update, since it has the correct type
console.log(
'action1 result',
reducer(state, action1)
);
// prints action2 result { a: 1, b: 2 }
// action2 is ignored, since it has the wrong type
console.log(
'action2 result',
reducer(state, action2)
);
ConditionalReducer
Kind: static method of async
Params
- condition function - a condition task.
- onTrue function - reducer to run if true.
- onFalse function - reducer to run if false.
async.InitState(initializer) ⇒ taskFunction
InitState accepts an initializer function/value, and builds an initial state if none exists
Kind: static method of async
Params
- initializer function - a condition task.
async.PostprocessState(reducer, postprocessor) ⇒ function
PostprocessState
Kind: static method of async
Params
- reducer function - the reducer to wrap around.
- postprocessor function - the postprocessor function to call after the reducer.
async.SeriesReducer(...reducers) ⇒ function
SeriesReducer accepts any number of reducers, and builds a new reducer that chains them together in series
Kind: static method of async
Params
- ...reducers function - all reducers.
async.SwitchReducer(...reducers) ⇒ function
Kind: static method of async
Params
- ...reducers function - all reducers.
reducer-patterns.ConditionalReducer(condition, onTrue, onFalse) ⇒ function
let ConditionalReducer = require('reducer-patterns/ConditionalReducer');
let state = {
a: 1,
b: 2,
};
let action1 = {
type: 'a',
payload: 2,
};
let action2 = {
type: 'b',
payload: 2,
};
let reducer = ConditionalReducer(
(state, action) => action.type === 'a',
(state, action) => ({
...state,
a: action.payload,
})
);
// prints action1 result { a: 2, b: 2 }
// action1 triggers a state update, since it has the correct type
console.log(
'action1 result',
reducer(state, action1)
);
// prints action2 result { a: 1, b: 2 }
// action2 is ignored, since it has the wrong type
console.log(
'action2 result',
reducer(state, action2)
);
ConditionalReducer
Kind: static method of reducer-patterns
Params
- condition function - a condition task.
- onTrue function - reducer to run if true.
- onFalse function - reducer to run if false.
reducer-patterns.InitState(initializer) ⇒ taskFunction
InitState accepts an initializer function/value, and builds an initial state if none exists
Kind: static method of reducer-patterns
Params
- initializer function - a condition task.
reducer-patterns.PostprocessState(reducer, postprocessor) ⇒ function
PostprocessState
Kind: static method of reducer-patterns
Params
- reducer function - the reducer to wrap around.
- postprocessor function - the postprocessor function to call after the reducer.
reducer-patterns.SeriesReducer(...reducers) ⇒ function
SeriesReducer accepts any number of reducers, and builds a new reducer that chains them together in series
Kind: static method of reducer-patterns
Params
- ...reducers function - all reducers.
reducer-patterns.SwitchReducer(...reducers) ⇒ function
Kind: static method of reducer-patterns
Params
- ...reducers function - all reducers.