Skip to content

Instantly share code, notes, and snippets.

@mrtnbroder
Last active January 16, 2018 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrtnbroder/319028e76211103c56e2ade438d2f86c to your computer and use it in GitHub Desktop.
Save mrtnbroder/319028e76211103c56e2ade438d2f86c to your computer and use it in GitHub Desktop.
Ramda Receips
import R from 'ramda'
const mergePlan = (x, y) => {
if(Array.isArray(x) && Array.isArray(y)) {
return R.uniq(R.concat(x, y));
}
if(typeof x === 'object' && typeof y === 'object'){
return R.mergeWith(mergePlan, x, y)
}
return y;
}
export const deepMerge = R.mergeWith(mergePlan)
// Usage:
// deepMerge(
// { baz: ['yolo'], foo: 'bar' },
// { baz: ['bar'], foo: 'yolo' }
// )
//
// Output:
// { 'baz': ['yolo', 'bar'], 'foo': 'yolo' }
import R from 'ramda'
// <A, B> (path: Array<string>, fn: (value: A) => A, (val: B) => B) => B
export const updatePath = R.curry((path, fn, val) =>
R.assocPath(path, fn(R.path(path, val)), val)
)
// Usage:
// updatePath(
// ['foo'],
// (val) => 'bar',
// { foo: 'yolo' }
// )
//
// Output:
// { 'foo': 'bar' }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment