Last active
July 10, 2018 07:53
-
-
Save tansongyang/4672da57507f403f3de69e546fa83dd9 to your computer and use it in GitHub Desktop.
An implementation of Ramda's `evolve` function in lodash.
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
// 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