Created
December 22, 2017 22:06
-
-
Save VandeurenGlenn/bda17c14507528ed03ad800d7c4c321a to your computer and use it in GitHub Desktop.
Quick object merging.
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
/** | |
* @export merge | |
* | |
* @param {object} object The object to merge with | |
* @param {object} source The object to merge | |
* @return {object} merge result | |
*/ | |
export const merge = (object = {}, source = {}) => { | |
// deep assign | |
for (const key of Object.keys(object)) { | |
if (source[key]) { | |
Object.assign(object[key], source[key]); | |
} | |
} | |
// assign the rest | |
for (const key of Object.keys(source)) { | |
if (!object[key]) { | |
object[key] = source[key]; | |
} | |
} | |
return object; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment