Skip to content

Instantly share code, notes, and snippets.

@david-mart
Created September 26, 2019 00:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-mart/3a07176df2bd4ed8c3a4576731a2e8db to your computer and use it in GitHub Desktop.
Save david-mart/3a07176df2bd4ed8c3a4576731a2e8db to your computer and use it in GitHub Desktop.
/* eslint-disable import/prefer-default-export */
import * as R from 'ramda';
/**
* @curried
* @param {Object} reducer The simplified reducer object, i.e. { increment: (state, payload) => state + payload }
* @param {Object} initialState Initial state for the reducer function
* @returns {Function} Generalized reducer using the Ramda Cond pattern (http://ramdajs.com/docs/#cond)
*/
export const reducerWrapper = (reducer, initialState) => (state = initialState, action) => R.pipe(
R.mapObjIndexed((func, type) => [
R.pipe(
R.prop('type'),
R.equals(type),
),
({ payload }) => func(payload)(state),
]),
R.values,
R.append([R.T, R.always(state)]),
R.call(R.cond),
)(reducer)(action);
/**
* @curried
* @param {String} type Redux action type (usually referencing types.js)
* @param {Any} payload Payload for specified function
* @returns {Object} Redux action (with type and payload)
*/
export const wrapActionPayload = type => R.pipe(
R.objOf('payload'),
R.assoc('type', type),
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment