UNPKG

@cookbook/dot-notation

Version:

Object readings and complex transformations using dot notation syntax.

28 lines 853 B
import is from './is'; import shallowCopy from './shallow-copy'; const merge = (x, y) => { if (!(is.object(x) || is.object(y))) { return y; } const lhs = shallowCopy(x); const rhs = shallowCopy(y); const keys = Object.keys(lhs); const content = shallowCopy(y); for (let i = 0; i < keys.length; i += 1) { const key = keys[i]; const lhsValue = shallowCopy(lhs[key]); const rhsValue = shallowCopy(rhs[key]); if (is.array(lhsValue) && is.array(rhsValue)) { content[key] = rhsValue; } else if (is.object(lhsValue) && is.object(rhsValue)) { content[key] = merge(lhsValue, rhsValue); } else { content[key] = rhsValue || lhsValue; } } return content; }; export default merge; //# sourceMappingURL=merge.js.map