35 lines
1.2 KiB
JavaScript
35 lines
1.2 KiB
JavaScript
|
"use strict";
|
||
|
Object.defineProperty(exports, "__esModule", { value: true });
|
||
|
exports.deepMerge = void 0;
|
||
|
const type_1 = require("./type");
|
||
|
/**
|
||
|
* Function that deep merges objects
|
||
|
*
|
||
|
* @copyright https://github.com/sindresorhus/ky/blob/b3c9e88fa49d50150dbb6e6b771b4af56cb40c98/source/utils/merge.ts
|
||
|
* @licence MIT
|
||
|
* @param sources
|
||
|
*/
|
||
|
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 ((0, type_1.isObject)(source)) {
|
||
|
// eslint-disable-next-line prefer-const
|
||
|
for (let [key, value] of Object.entries(source)) {
|
||
|
if ((0, type_1.isObject)(value) && key in returnValue) {
|
||
|
value = deepMerge(returnValue[key], value);
|
||
|
}
|
||
|
returnValue = Object.assign(Object.assign({}, returnValue), { [key]: value });
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return returnValue;
|
||
|
}
|
||
|
exports.deepMerge = deepMerge;
|