UNPKG

@modern-kit/utils

Version:
38 lines (35 loc) 1.31 kB
import { isPlainObject } from '../../validator/isPlainObject/index.mjs'; import { cloneDeep } from '../../common/cloneDeep/index.mjs'; import { objectKeys } from '../objectKeys/index.mjs'; import '../../validator/isReference/index.mjs'; import '../../validator/isPrimitive/index.mjs'; function merge(target, source) { const mergedTarget = cloneDeep(target); const innerMerge = (target2, source2) => { const sourceKeys = objectKeys(source2); for (let i = 0; i < sourceKeys.length; i++) { const key = sourceKeys[i]; const sourceValue = source2[key]; const targetValue = target2[key]; if (Array.isArray(sourceValue)) { if (Array.isArray(targetValue)) { target2[key] = innerMerge(targetValue, sourceValue); } else { target2[key] = innerMerge([], sourceValue); } } else if (isPlainObject(sourceValue)) { if (isPlainObject(targetValue)) { target2[key] = innerMerge(targetValue, sourceValue); } else { target2[key] = innerMerge({}, sourceValue); } } else if (targetValue === void 0 || sourceValue !== void 0) { target2[key] = sourceValue; } } return target2; }; return innerMerge(mergedTarget, source); } export { merge }; //# sourceMappingURL=index.mjs.map