Skip to content

Instantly share code, notes, and snippets.

@winkerVSbecks
Created May 9, 2017 18:53
Show Gist options
  • Select an option

  • Save winkerVSbecks/4ee863fff8f000adbd49d516122f172e to your computer and use it in GitHub Desktop.

Select an option

Save winkerVSbecks/4ee863fff8f000adbd49d516122f172e to your computer and use it in GitHub Desktop.
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,
);
}
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