Last active
January 16, 2018 09:04
-
-
Save mrtnbroder/319028e76211103c56e2ade438d2f86c to your computer and use it in GitHub Desktop.
Ramda Receips
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
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' } |
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
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