Skip to content

Instantly share code, notes, and snippets.

@tansongyang
Last active July 10, 2018 07:53
Show Gist options
  • Save tansongyang/4672da57507f403f3de69e546fa83dd9 to your computer and use it in GitHub Desktop.
Save tansongyang/4672da57507f403f3de69e546fa83dd9 to your computer and use it in GitHub Desktop.
An implementation of Ramda's `evolve` function in lodash.
// http://stackoverflow.com/questions/38090023/whats-the-lodash-fp-equivalent-of-ramdas-evolve-function/38425764#38425764
const mapValuesWithKey = _.mapValues.convert({cap: false});
function evolve(transformations) {
return item =>
mapValuesWithKey((value, key) => {
const transformation = _.getOr(_.identity)(key)(transformations);
const type = typeof transformation;
return type === 'function' ?
transformation(value) :
transformation && type === 'object' ?
evolve(transformation)(value) :
value;
})(item);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment