Implement deepMerge(...objects). It merges any number of plain objects left to right into a brand-new object. Nested plain objects are merged recursively instead of being replaced wholesale.
This is how theme systems (createTheme(base, overrides)), bundler configs and feature-flag defaults combine layered settings.
const defaults = { theme: { color: 'blue', spacing: { unit: 8 } }, plugins: ['a'], debug: false };
const user = { theme: { spacing: { unit: 4 } }, plugins: ['b'], debug: undefined };
deepMerge(defaults, user);
// {
// theme: { color: 'blue', spacing: { unit: 4 } },
// plugins: ['b'], // arrays are replaced, not merged
// debug: false, // undefined in a later object does not overwrite
// }
deepMerge(...objects) // returns a new plain object
Object.prototype or null), merge them recursively.merge merges arrays by index; we deliberately don't.)undefined is skipped (the existing value is kept). null does overwrite.Date, Map, class instances, functions) are copied by reference.null/undefined arguments are ignored. With no arguments, return {}.__proto__ (prototype-pollution guard).'replace' | 'concat' | 'byIndex').deepMerge behave with circular references, and how would you fix it?merge a common source of prototype-pollution CVEs?Spotted a mistake or a rights concern? Report an issue