@limlabs/limo
Version:
Infrastructure as Code generator
36 lines (35 loc) • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.deepMerge = void 0;
function isObject(item) {
return item && typeof item === "object" && !Array.isArray(item);
}
/**
* Deeply merges the properties of the source object into the target object.
* If both target and source are objects, it recursively merges their properties.
* If both target and source are arrays, it concatenates the source array into the target array.
*
* @param target - The target object or array to merge properties into.
* @param source - The source object or array to merge properties from.
* @returns The modified target object or array.
*/
function deepMerge(target, source) {
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isObject(source[key])) {
if (!target[key]) {
Object.assign(target, { [key]: {} });
}
deepMerge(target[key], source[key]);
}
else {
Object.assign(target, { [key]: source[key] });
}
}
}
else if (Array.isArray(target) && Array.isArray(source)) {
target.push(...source);
}
return target;
}
exports.deepMerge = deepMerge;