UNPKG

deepmerge-plus

Version:

用於深度(遞迴)合併 JavaScript 物件的函式庫 / A library for deep (recursive) merging of JavaScript objects

143 lines (127 loc) 4.09 kB
import e from "is-mergeable-object"; function _shouldClone(e) { return !1 !== (null == e ? void 0 : e.clone); } function _defaultCheckShouldNotUpsertValue(e, t, r, n) { var l; return !_isUndefined(null === (l = r.target) || void 0 === l ? void 0 : l[r.key]); } function cloneUnlessOtherwiseSpecified(e, t, r, n) { const l = _shouldClone(t) && _isMergeableObject(e, t, r, n); let o = l ? deepmerge(function emptyTarget(e) { return Array.isArray(e) ? [] : {}; }(e), e, t, n) : e; if (null != t && t.keyValueOrMode && !l && r && "key" in r && (r.destination && (o = r.destination[r.key] || o), r.target && (o = r.target[r.key] || o), r.source && (o = r.source[r.key] || o)), null != t && t.keyValueUpsertMode && !l && r && "key" in r) { let l = !1; if ("function" == typeof t.keyValueUpsertMode) l = t.keyValueUpsertMode(e, t, r, n); else if (!0 === t.keyValueUpsertMode) { var i; l = !_isUndefined(null === (i = r.target) || void 0 === i ? void 0 : i[r.key]); } if (l) { let e; r.destination && (null != e || (e = r.destination[r.key])), r.target && (null != e || (e = r.target[r.key])), o = e; } } return o; } function _isUndefined(e) { return void 0 === e; } function _isNull(e) { return null === e; } function _isNullOrUndefined(e) { return null == e; } function _isMergeableObject(r, n, l, o) { var i; let a = null == n || null === (i = n.isMergeableObject) || void 0 === i ? void 0 : i.call(n, r, e, n, l, o); return null == a && (a = "boolean" == typeof (null == r ? void 0 : r[t]) ? r[t] : e(r)), a; } function defaultArrayMerge(e, t, r, n) { null != n || (n = _newTmpRuntimeData([])); const l = n.level + 1, o = n.root, i = []; return n.parent[n.key] = i, e.concat(t).reduce((e, t, i) => (t = cloneUnlessOtherwiseSpecified(t, r, { key: i }, { level: l, paths: [ ...n.paths, i ], root: o, parent: e, key: i }), e[i] = t, e), i); } function _handleOptions(e) { return e || {}; } function deepmerge(e, t, r, n) { const l = Array.isArray(t), o = Array.isArray(e), i = _handleOptions(r); var a; return l === o ? l ? (null !== (a = null == i ? void 0 : i.arrayMerge) && void 0 !== a ? a : defaultArrayMerge)(e, t, r, n) : function mergeObject(e, t, r, n) { let l = {}; return n || (n = _newTmpRuntimeData(l)), _isMergeableObject(e, r, void 0, n) && Object.keys(e).forEach(function(o) { l[o] = cloneUnlessOtherwiseSpecified(e[o], r, { key: o, source: t, target: e, destination: l }, { level: n.level + 1, paths: [ ...n.paths, o ], root: n.root, parent: l, key: o }); }), Object.keys(t).forEach(function(o) { l[o] = _isMergeableObject(t[o], r, { key: o, source: t, target: e }, n) && e[o] ? deepmerge(e[o], t[o], r, { level: n.level + 1, paths: [ ...n.paths, o ], root: n.root, parent: l, key: o }) : cloneUnlessOtherwiseSpecified(t[o], r, { key: o, source: t, target: e }, { level: n.level + 1, paths: [ ...n.paths, o ], root: n.root, parent: l, key: o }); }), l; }(e, t, r, n) : cloneUnlessOtherwiseSpecified(t, r, { target: e, source: t }, n); } function isMergeable(t) { return e(t); } const t = /*#__PURE__*/ Symbol.for("SYMBOL_IS_MERGEABLE"); function _newTmpRuntimeData(e) { return { level: 0, paths: [], root: e, parent: e, key: void 0 }; } function deepmergeAll(e, t) { if (!Array.isArray(e)) throw new Error("first argument should be an array"); return e.reduce(function(e, r) { return deepmerge(e, r, t); }, {}); } export { t as SYMBOL_IS_MERGEABLE, _defaultCheckShouldNotUpsertValue, _handleOptions, _isMergeableObject, _isNull, _isNullOrUndefined, _isUndefined, _newTmpRuntimeData, _shouldClone, deepmergeAll as all, deepmerge, deepmergeAll, deepmerge as default, isMergeable }; //# sourceMappingURL=index.esm.mjs.map