Created
January 24, 2019 12:48
-
-
Save mertenvg/1ac3b95ca48a5b50755e2ce8aec6d98d to your computer and use it in GitHub Desktop.
Typescript deep merge function
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
function merge(a: any, b: any): any { | |
return (a !== Object(a) || b !== Object(b)) | |
? b | |
: Object.keys(b).filter((k: string) => !a[k]).reduce( | |
(o: { [i: string]: any }, k: string) => { | |
o[k] = b[k]; | |
return o; | |
}, | |
Object.keys(a).reduce( | |
(o: { [i: string]: any }, k: string) => { | |
o[k] = b[k] | |
? merge(a[k], b[k]) | |
: a[k]; | |
return 0; | |
}, | |
{} | |
) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment