fix(common): Options merge side effect

This commit is contained in:
Marcos Susaña 2023-06-29 01:41:09 -04:00
parent d05a89b064
commit 75401227ca

View File

@ -23,25 +23,28 @@ export const Options = <T = any>(defaults: any, ...options: any[]): T => {
const source = options.shift(); const source = options.shift();
if (isObject(defaults) && isPlainObject(source)) { // This prevents default options from being intercepted by `Object.assign`
const $ = { ...defaults };
if (isObject($) && isPlainObject(source)) {
Object.entries(source).forEach(([key, value]) => { Object.entries(source).forEach(([key, value]) => {
if (typeof value === 'undefined') { if (typeof value === 'undefined') {
return; return;
} }
if (isPlainObject(value)) { if (isPlainObject(value)) {
if (!(key in defaults)) { if (!(key in $)) {
Object.assign(defaults, { [key]: {} }); Object.assign($, { [key]: {} });
} }
Options(defaults[key], value); Options($[key], value);
} else { } else {
Object.assign(defaults, { [key]: value }); Object.assign($, { [key]: value });
} }
}); });
} }
return Options(defaults, ...options); return Options($, ...options);
}; };
/** /**
* Convert a camelCase object to snake_case. * Convert a camelCase object to snake_case.