ts-japi
Version:
A highly-modular (typescript-friendly)-framework agnostic library for serializing data to the JSON:API specification
30 lines • 1.05 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const is_plain_object_1 = require("./is-plain-object");
/**
* Deep merge two objects over their enumerable properties.
*
* @param target - The object to merge into
* @param source - The objects to use for merging
*/
function merge(target, ...sources) {
if (!sources.length)
return target;
const source = sources.shift();
if ((0, is_plain_object_1.isPlainObject)(target) && (0, is_plain_object_1.isPlainObject)(source)) {
for (const key of Object.keys(source)) {
const sourceItem = source[key];
if ((0, is_plain_object_1.isPlainObject)(sourceItem)) {
if (!target[key])
Object.assign(target, { [key]: {} });
merge(target[key], sourceItem);
}
else {
Object.assign(target, { [key]: source[key] });
}
}
}
return merge(target, ...sources);
}
exports.default = merge;
//# sourceMappingURL=merge.js.map