1 | import { __assign, __spreadArray } from "tslib";
|
2 | import { isNonNullObject } from "./objects.js";
|
3 | var hasOwnProperty = Object.prototype.hasOwnProperty;
|
4 | export function mergeDeep() {
|
5 | var sources = [];
|
6 | for (var _i = 0; _i < arguments.length; _i++) {
|
7 | sources[_i] = arguments[_i];
|
8 | }
|
9 | return mergeDeepArray(sources);
|
10 | }
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | export function mergeDeepArray(sources) {
|
18 | var target = sources[0] || {};
|
19 | var count = sources.length;
|
20 | if (count > 1) {
|
21 | var merger = new DeepMerger();
|
22 | for (var i = 1; i < count; ++i) {
|
23 | target = merger.merge(target, sources[i]);
|
24 | }
|
25 | }
|
26 | return target;
|
27 | }
|
28 | var defaultReconciler = function (target, source, property) {
|
29 | return this.merge(target[property], source[property]);
|
30 | };
|
31 | var DeepMerger = (function () {
|
32 | function DeepMerger(reconciler) {
|
33 | if (reconciler === void 0) { reconciler = defaultReconciler; }
|
34 | this.reconciler = reconciler;
|
35 | this.isObject = isNonNullObject;
|
36 | this.pastCopies = new Set();
|
37 | }
|
38 | DeepMerger.prototype.merge = function (target, source) {
|
39 | var _this = this;
|
40 | var context = [];
|
41 | for (var _i = 2; _i < arguments.length; _i++) {
|
42 | context[_i - 2] = arguments[_i];
|
43 | }
|
44 | if (isNonNullObject(source) && isNonNullObject(target)) {
|
45 | Object.keys(source).forEach(function (sourceKey) {
|
46 | if (hasOwnProperty.call(target, sourceKey)) {
|
47 | var targetValue = target[sourceKey];
|
48 | if (source[sourceKey] !== targetValue) {
|
49 | var result = _this.reconciler.apply(_this, __spreadArray([target,
|
50 | source,
|
51 | sourceKey], context, false));
|
52 |
|
53 |
|
54 | if (result !== targetValue) {
|
55 | target = _this.shallowCopyForMerge(target);
|
56 | target[sourceKey] = result;
|
57 | }
|
58 | }
|
59 | }
|
60 | else {
|
61 |
|
62 |
|
63 | target = _this.shallowCopyForMerge(target);
|
64 | target[sourceKey] = source[sourceKey];
|
65 | }
|
66 | });
|
67 | return target;
|
68 | }
|
69 |
|
70 | return source;
|
71 | };
|
72 | DeepMerger.prototype.shallowCopyForMerge = function (value) {
|
73 | if (isNonNullObject(value)) {
|
74 | if (!this.pastCopies.has(value)) {
|
75 | if (Array.isArray(value)) {
|
76 | value = value.slice(0);
|
77 | }
|
78 | else {
|
79 | value = __assign({ __proto__: Object.getPrototypeOf(value) }, value);
|
80 | }
|
81 | this.pastCopies.add(value);
|
82 | }
|
83 | }
|
84 | return value;
|
85 | };
|
86 | return DeepMerger;
|
87 | }());
|
88 | export { DeepMerger };
|
89 |
|
\ | No newline at end of file |