Skip to content

Instantly share code, notes, and snippets.

@mruhlin
Created November 15, 2017 14:52
Show Gist options
  • Save mruhlin/26f41dff7b9e8579e70cf56e19885bba to your computer and use it in GitHub Desktop.
Save mruhlin/26f41dff7b9e8579e70cf56e19885bba to your computer and use it in GitHub Desktop.
/**
* Converts a map of action keys into redux-dispatchable string values,
* so you don't have to type the action name a bunch of times.
*
* Example:
*
* let actions = {
* EAT_TACO: null,
* EAT_BURRITO: null,
*
* SPECIAL_SAUCE: 'really_its_just_mayo' // you can still specify a name if you want.
* };
* actions = actionize(actions, (action) => `texmex/${action}`);
*
* actions == {
* EAT_TACO: 'texmex/EAT_TACO',
* EAT_BURRITO: 'texmex/EAT_BURRITO',
* SPECIAL_SAUCE: 'really_its_just_mayo'
* }
*
*
* @param {Object} actions - Map of action keys to their names or null to generate a name.
* @param {function} makeAction - Generator function that takes an action key and returns a string to use (i.e. (action) => `prefix/${action}`)
* @returns compare function for use with Array.prototype.sort
*/
export default (actions, makeAction) => {
const actionKeys = Object.keys(actions);
let ret = {};
actionKeys.forEach((key) => {
ret[key] = actions[key] || makeAction(key)
});
return ret;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment