Skip to content

Instantly share code, notes, and snippets.

@phpsmarter
Forked from mrtnbroder/deep-merge.js
Created January 16, 2018 09:04
Show Gist options
  • Save phpsmarter/7d017d9b5489efc5d9362821b33deeaa to your computer and use it in GitHub Desktop.
Save phpsmarter/7d017d9b5489efc5d9362821b33deeaa 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