Skip to content

Instantly share code, notes, and snippets.

@david-mart
Created December 19, 2018 06:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save david-mart/73d69152578466181f6094b1ed648abf to your computer and use it in GitHub Desktop.
Save david-mart/73d69152578466181f6094b1ed648abf to your computer and use it in GitHub Desktop.
Ramda utilities
// 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