Created
June 25, 2017 23:18
-
-
Save Tiny-Giant/43cc03adf3cdc84ff935655cbebbe585 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const deepmerge = (h, ...args) => { | |
const gettype = e => Object.prototype.toString.call(e).replace(/.*\b(\w+)./, '$1'); | |
const o = {}; | |
Object.assign(h, { | |
"Array": (a, b) => [...a, ...b], | |
"Object": (a, b) => deepmerge(h, a, b), | |
"Set": (a, b) => new Set([...a, ...b]), | |
"Map": (a, b) => { | |
const o = new Map([...a]); | |
for(let [k,v] of [...b]) { | |
const vt = gettype(v); | |
o.set(k, a.has(k) && vt in h ? h[vt](a.get(k), v) : v); | |
} | |
return o; | |
} | |
}, h); | |
const p = new Proxy(o, { | |
set: (o, k, b) => { | |
const a = o[k]; | |
const at = gettype(a); | |
return (o[k] = (at in h && k in o ? h[at](a, b) : b), true); | |
} | |
}); | |
Object.assign(p, ...args); | |
return o; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment