reduce-key
Reduce a keyed map (or array), using a selector function.
Installation
$ npm install @f/reduce-key
Usage
One thing that often prevents the fully declarative construction of reducers is keyed maps (e.g. objects of the form {[key]: entity}
, or arrays). reduceKey solves this problem, by allowing you to specify a selector function that decides which subject to apply the reducer to.
var reduceKey = require('@f/reduce-key')
const todoReducer = combineReducers({
text: handleActions({
[TODO_SET_TEXT]: (state, {text}) => text,
[TODO_CLEAR_TEXT]: () => ''
}),
completed: handleActions({
[TOGGLE_COMPLETED]: (state) => !state
})
})
export default combineReducers({
todos: reduceKey((state, {idx}) => idx, todoReducer)
})
API
reduceKey(select, reduce)
-
select
- A function that accepts state and action and returns the key. Ifselect
returnsnull
,undefined
, orfalse
, the reducer will not be called. So you are free to do things like:(state, action) => action.payload.key
- even if your actions do not always contain key. -
reduce
- A reducer that reduces over the entities within the map or array.
Returns: A reducer that reduces over the sub-object in the key and returns a new copy of the map, with the sub-object specified by select(state, action)
updated by your reducer.
License
MIT