Creating reducer with object properties more object oriented by definition than traditional switch-case procedural without any dependencies


101.0.14 years ago4 years agoMinified + gzip package size for @ikhsaan/create-reducer in KB


Create Reducer


Read this ebook Programming Javascript Applications and find 'Don't Use switch'


npm i -S @ikhsaan/create-reducer

// or

yarn add @ikhsaan/create-reducer


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

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.