@ikhsaan/create-reducer
v1.0.1
Published
Creating reducer with object properties more object oriented by definition than traditional switch-case procedural without any dependencies
Downloads
39
Maintainers
Readme
Create Reducer
Behind
Read this ebook Programming Javascript Applications and find 'Don't Use switch
'
Installation
npm i -S @ikhsaan/create-reducer
// or
yarn add @ikhsaan/create-reducer
Usage
NOTE: Assume you use seamless-immutable / immutable-js. otherwise use spread object instead
import createReducer from '@ikhsaan/create-reducer'
createReducer(INITIAL_STATE, {
[FAILURE](state, { error }) {
return state.set('error', error);
},
[INCREMENT](state) {
return state.update('count', increment);
},
[LOAD_DATA_SUCCESS](state, action) {
return state.merge({ placeholderData: action.data });
},
[TICK_CLOCK](state, action) {
return state.set('lastUpdate', action.ts).set('light', !!action.light);
},
});
// or
const failure = (state, { error }) => state.set('error', error);
const updateTick = (state, action) => state.set('lastUpdate', action.ts).set('light', !!action.light);
createReducer(INITIAL_STATE, {
[FAILURE]: failure,
[TICK_CLOCK]: updateTick
});