Created
December 19, 2018 06:01
-
-
Save david-mart/73d69152578466181f6094b1ed648abf to your computer and use it in GitHub Desktop.
Ramda utilities
This file contains 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
// applying spec of functions to initial passed object | |
const mergeSpec = R.curry(function mergeSpec(spec, value) { | |
return R.converge(R.merge, [R.identity, R.applySpec(spec)])(value) | |
}) | |
// now we can combine data from different props and apply it to the original object | |
const getFullName = R.pipe(R.pick(['first_name', 'last_name']), R.values, R.join(' ')) | |
const combineLikesAndComments = R.converge(R.concat, [ | |
R.propOr([], 'likes'), | |
R.propOr([], 'comments') | |
]) | |
// now simply create a spec similar to R.applySpec | |
const updateUser = mergeSpec({ full_name: getFullName, likesAndComments: combineLikesAndComments}) | |
// now just call the new function on the user object | |
updateUser({ | |
first_name: 'John', | |
last_name: 'Doe', | |
likes: ['sun', 'moon'], | |
comments: ['hello', 'world'] | |
}) | |
// Will return | |
// | |
// { | |
// "comments": ["hello", "world"], | |
// "first_name": "John", | |
// "last_name": "Doe", | |
// "likes": ["sun", "moon"], | |
// "full_name": "John Doe", // -- combined name | |
// "likesAndComments": ["sun", "moon", "hello", "world"] // -- combined lists | |
// } | |
// as you can imaging, possibilities are just endless here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment