@ozen-ui/kit
Version:
React component library
44 lines (43 loc) • 1.66 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.mergeDeep = void 0;
var tslib_1 = require("tslib");
var isPlainObject_1 = require("../isPlainObject");
function deepClone(source) {
if (!(0, isPlainObject_1.isPlainObject)(source)) {
return source;
}
var output = {};
Object.keys(source).forEach(function (key) {
output[key] = deepClone(source[key]);
});
return output;
}
var mergeDeep = function (target, source, options) {
if (options === void 0) { options = { clone: true }; }
var output = options.clone ? tslib_1.__assign({}, target) : target;
if ((0, isPlainObject_1.isPlainObject)(target) && (0, isPlainObject_1.isPlainObject)(source)) {
Object.keys(source).forEach(function (key) {
// Avoid prototype pollution
if (key === '__proto__') {
return;
}
if ((0, isPlainObject_1.isPlainObject)(source[key]) &&
key in target &&
(0, isPlainObject_1.isPlainObject)(target[key])) {
// Since `output` is a clone of `target` and we have narrowed `target` in this block we can cast to the same type.
output[key] = (0, exports.mergeDeep)(target[key], source[key], options);
}
else if (options.clone) {
output[key] = (0, isPlainObject_1.isPlainObject)(source[key])
? deepClone(source[key])
: source[key];
}
else {
output[key] = source[key];
}
});
}
return output;
};
exports.mergeDeep = mergeDeep;
;