@ozen-ui/kit
Version:
React component library
40 lines (39 loc) • 1.38 kB
JavaScript
import { __assign } from "tslib";
import { isPlainObject } from '../isPlainObject';
function deepClone(source) {
if (!isPlainObject(source)) {
return source;
}
var output = {};
Object.keys(source).forEach(function (key) {
output[key] = deepClone(source[key]);
});
return output;
}
export var mergeDeep = function (target, source, options) {
if (options === void 0) { options = { clone: true }; }
var output = options.clone ? __assign({}, target) : target;
if (isPlainObject(target) && isPlainObject(source)) {
Object.keys(source).forEach(function (key) {
// Avoid prototype pollution
if (key === '__proto__') {
return;
}
if (isPlainObject(source[key]) &&
key in target &&
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] = mergeDeep(target[key], source[key], options);
}
else if (options.clone) {
output[key] = isPlainObject(source[key])
? deepClone(source[key])
: source[key];
}
else {
output[key] = source[key];
}
});
}
return output;
};