36 lines
964 B
JavaScript
36 lines
964 B
JavaScript
|
import { isObject } from "./type.js";
|
||
|
/**
|
||
|
* Function that deep merges objects
|
||
|
*
|
||
|
* @copyright https://github.com/sindresorhus/ky/blob/b3c9e88fa49d50150dbb6e6b771b4af56cb40c98/source/utils/merge.ts
|
||
|
* @licence MIT
|
||
|
* @param sources
|
||
|
*/
|
||
|
|
||
|
export function deepMerge(...sources) {
|
||
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||
|
let returnValue = {};
|
||
|
|
||
|
for (const source of sources) {
|
||
|
if (Array.isArray(source)) {
|
||
|
if (!Array.isArray(returnValue)) {
|
||
|
returnValue = [];
|
||
|
}
|
||
|
|
||
|
returnValue = [...returnValue, ...source];
|
||
|
} else if (isObject(source)) {
|
||
|
// eslint-disable-next-line prefer-const
|
||
|
for (let [key, value] of Object.entries(source)) {
|
||
|
if (isObject(value) && key in returnValue) {
|
||
|
value = deepMerge(returnValue[key], value);
|
||
|
}
|
||
|
|
||
|
returnValue = Object.assign(Object.assign({}, returnValue), {
|
||
|
[key]: value
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return returnValue;
|
||
|
}
|