File size: 663 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import isObject from './isObject';
import isPrimitive from './isPrimitive';
export default function deepMerge<
T extends Record<keyof T, any>,
U extends Record<keyof U, any>,
>(target: T, source: U): T & U {
if (isPrimitive(target) || isPrimitive(source)) {
return source;
}
for (const key in source) {
const targetValue = target[key];
const sourceValue = source[key];
try {
target[key] =
(isObject(targetValue) && isObject(sourceValue)) ||
(Array.isArray(targetValue) && Array.isArray(sourceValue))
? deepMerge(targetValue, sourceValue)
: sourceValue;
} catch {}
}
return target;
}
|