deduce
Ridiculously easy JavaScript state containers with reducer methods. Like Redux without all of the boilerplate.
Install
npm install --save deduce
Usage
// reducers.js { return state + val;} { return ;} // store.js ;; const store = ; store; store; // -> 2store; // -> 4store; // -> 3store; // -> 1
API
deduce(initialState, reducers) : Store
initialState
{*}
reducers
{Object<String,Function>}
Store
.state
Current state of the store.
const store = ; console; // -> { foo: 1 }
.addReducers(reducers): Store
reducers
{Object<String,Function>}
Registers reducers to modify the state. Chainable.
store;
.addReducersFor(property, reducers): Store
property
{String}
reducers
{Object<String,Function>}
Registers reducers to modify a specific state property. Chainable.
store;
.addListener(callback): Function
callback
{Function}
Adds a listener to be called any time the state is updated. Returns a function to remove the listener.
const removeListener = store; store;
Why?
The typical Redux patterns entail a lot of boilerplate. The documented and accepted patterns for reducing boilerplate really just swap one kind for another:
Redux Example
Consider the following Redux example that creates a store with two numbers: foo
which may be incremented and bar
which may be decremented.
// foo const FOO_INCREMENT = 'FOO_INCREMENT'; const fooInitial = 0; const fooReducers = FOO_INCREMENT: state = fooInitial action return state + actionpayload; ; { if actiontype in fooReducers return fooReducersactiontypestate action; return state;} { return type: FOO_INCREMENT payload ;} // bar const BAR_DECREMENT = 'BAR_DECREMENT'; const barInitial = 0; const barReducers = BAR_DECREMENT: state = barInitial action return state - actionpayload; ; { if actiontype in barReducers return barReducersactiontypestate action; return state;} { return type: BAR_DECREMENT payload ;} // store ; const reducer = ;const store = ; // application store;store; console;// {// foo: 1,// bar: -1// }
Split that up into modules and you can see how new-comers could easily be overwhelmed when the underlying principles are beautifully clean and simple.
Deduce Example
Compare the above with this deduce
example that does the same thing:
// foo const fooInitial = 0; const fooReducers = { return state + val; }; // bar const barInitial = 0; const barReducers = { return state - val; }; // store ; const store = ; // application store;store; console;// {// foo: 1,// bar: -1// }
Contribute
Standards for this project, including tests, code coverage, and semantics are enforced with a build tool. Pull requests must include passing tests with 100% code coverage and no linting errors.
Test
$ npm test
MIT © Shannon Moeller