Skip to content

A tiny library for creating reducer with object properties, due to more object oriented by definition than traditional switch-case procedural without any dependencies

Notifications You must be signed in to change notification settings

ikhsanalatsary/createreducer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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
});

About

A tiny library for creating reducer with object properties, due to more object oriented by definition than traditional switch-case procedural without any dependencies

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages