Essential actions and reducers to be used with Redux.
It creates a values
entry in the state that can be mutated via 4 actions:
ADD VALUE
: adds an element to an array at a give path at a given positionDEL VALUE
: deletes an object property or removes an element from an arrayMOVE_VALUE
: moves an element within an arraySET VALUE
: sets a value at a give pathBATCH
: can be used to dispatch multiple actions at once
Action objects are created by calling the methods in the actions
module.
The state is treated as proper immutable by this library.
import assert from 'assert';
import { combineReducers, createStore } from 'redux';
import { actions, reducers } from 'redux-essentials';
// creates a brand new store
const store = createStore(combineReducers(reducers));
// set a value deep in the state
store.dispatch(actions.setValue(['cat', 'name'], 'Melinda'));
// throw if cat name isn't 'Melinda'
assert.equal(store.getState().values.cat.name, 'Melinda');
// set a value of type array deep in the state
store.dispatch(actions.setValue(['cat', 'food'], []));
// actions batch
store.dispatch(batch([
// append some values to the array
actions.addValue(['cat', 'food'], 'biscuit'),
actions.addValue(['cat', 'food'], 'meat'),
// insert a value at a specific position in the array
actions.addValue(['cat', 'food'], 'fish', 1)),
]));
// throw if food isn't an array exactly like expected
assert.deepEqual(store.getState().values.cat.food, ['biscuit','fish','meat']);
// move meat to the beginning of the array
store.dispatch(actions.moveValue(['cat', 'food'], 2, 0));
// remove 2nd item (biscuit) from the array
store.dispatch(actions.delValue(['cat', 'food'], 1));
// throw if food isn't an array exactly like expected
assert.deepEqual(store.getState().values.cat.food, ['meat','fish']);
Bill Balmant |