Created
June 10, 2016 21:29
-
-
Save Istar-Eldritch/e2211246a9531376d0a01ea44b054b41 to your computer and use it in GitHub Desktop.
Ramda deep merge.
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
var R = require('ramda') | |
var one = { | |
test: { | |
a: 1, | |
b: 2 | |
}, | |
pemento: "sipi", | |
arr: ['a', 'b'] | |
} | |
var two = { | |
test: { | |
a:3, | |
c:4 | |
}, | |
pemento: "nope", | |
arr: ['b', 'c'] | |
} | |
function deepMerge(v1, v2) { | |
if(Array.isArray(v1) && Array.isArray(v2)) { | |
return R.uniq(R.concat(v1, v2)); | |
} | |
else if(typeof v1 === 'object' && typeof v2 === 'object'){ | |
return R.mergeWith(deepMerge, v1, v2) | |
} | |
else { | |
return v2; | |
} | |
} | |
R.mergeWith(deepMerge,one, two) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The second
if
statement should also check ifv1
andv2
arenull
, becausetypeof null === 'object'
.