Created
May 9, 2017 18:53
-
-
Save winkerVSbecks/4ee863fff8f000adbd49d516122f172e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import R from 'ramda'; | |
| const actionType = value => R.compose( | |
| R.equals(value), | |
| R.propOr(undefined, 'type'), | |
| R.nthArg(1), | |
| ); | |
| const reducerConds = R.compose( | |
| R.cond, | |
| R.append([R.T, R.identity]), | |
| R.map(R.adjust(actionType, 0)), | |
| ); | |
| export function reducer(...conditions) { | |
| return initialState => | |
| (state, action) => reducerConds(conditions)( | |
| R.defaultTo(initialState, state), | |
| action, | |
| ); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const initialState = { foo: 12, bar: 14, baz: 16 }; | |
| const testReducer = reducer( | |
| ['FOO', (state, { payload }) => R.assoc('foo', payload, state)], | |
| ['BAR', (state, { payload }) => R.assoc('bar', payload, state)], | |
| ['BAZ', (state, { payload }) => R.assoc('baz', payload, state)], | |
| )(initialState); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment