25 lines
1.0 KiB
JavaScript
25 lines
1.0 KiB
JavaScript
import { entries, isArray, isPlainObject } from './helper.js';
|
|
/** Deep merge objects to the first one */
|
|
export const deepAssign = (originObject, ...overrideObjects) => {
|
|
if (overrideObjects.length === 0)
|
|
return originObject;
|
|
/** Object being merged */
|
|
const assignObject = overrideObjects.shift() || null;
|
|
if (assignObject)
|
|
entries(assignObject).forEach(([property, value]) => {
|
|
if (property === '__proto__' || property === 'constructor')
|
|
return;
|
|
if (isPlainObject(originObject[property]) && isPlainObject(value))
|
|
deepAssign(originObject[property], value);
|
|
else if (isArray(value))
|
|
originObject[property] = [...value];
|
|
else if (isPlainObject(value))
|
|
originObject[property] = {
|
|
...value,
|
|
};
|
|
else
|
|
originObject[property] = assignObject[property];
|
|
});
|
|
return deepAssign(originObject, ...overrideObjects);
|
|
};
|