Created
September 26, 2019 00:05
-
-
Save david-mart/3a07176df2bd4ed8c3a4576731a2e8db 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
/* 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