Created
November 3, 2020 11:14
-
-
Save sihu/0cb5b7d69e2bb6c3744da0cfcae7885d to your computer and use it in GitHub Desktop.
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
/** | |
Examples: | |
overrideDefaultConfig({ a: 1, b: 2 }, { a: 2 }); // => { a: 2, b: 2 } | |
overrideDefaultConfig({ a: { b: { c: 2 }}}, { a: { b: { c: 3, d: 4 }}}); // => { a: { b: { c: 3 } } } | |
overrideDefaultConfig({ a: 1, b: [2, 3]}, { a: 2, b: [4, 5] }); // => { a: 2, b: [4, 5]} | |
* This returns a new copy of the defaults and assigns all existing properties on the settings to the default config. | |
* @param {*} inputArg | |
* @returns {object} | |
*/ | |
const overrideDefaultConfig = (defaults, settings) => Object.keys(defaults).reduce((newObject, property) => { | |
if(typeof defaults[property] === "object" && !Array.isArray(defaults[property])) { | |
newObject[property] = overrideDefaultConfig(defaults[property], settings[property]); | |
} else { | |
newObject[property] = settings.hasOwnProperty(property) ? settings[property] : defaults[property]; | |
} | |
return newObject; | |
}, {}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment