@techmely/utils
Version:
Collection of helpful JavaScript / TypeScript utils
37 lines (34 loc) • 920 B
JavaScript
import { isObject } from './chunk-MGQF5GXR.mjs';
import { isArray } from './chunk-UGDKU24C.mjs';
// src/mergeDeep.ts
function mergeDeep(target, ...sources) {
if (sources.length === 0) {
return target;
}
const source = sources.shift();
if (source === void 0) {
return target;
}
if (isMergeableObject(target) && isMergeableObject(source)) {
const sourceKeys = Object.keys(source);
for (const key of sourceKeys) {
if (isMergeableObject(source[key])) {
if (!target[key]) {
target[key] = {};
}
if (isMergeableObject(target[key])) {
mergeDeep(target[key], source[key]);
} else {
target[key] = source[key];
}
} else {
target[key] = source[key];
}
}
}
return mergeDeep(target, ...sources);
}
function isMergeableObject(item) {
return isObject(item) && !isArray(item);
}
export { mergeDeep };