Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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