1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.merge = void 0;
|
19 |
|
20 | const lodash_merge_1 = require("./lodash.merge");
|
21 | const MAX_LEVEL = 20;
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function merge(...args) {
|
27 | let result = args.shift();
|
28 | const objects = new WeakMap();
|
29 | while (args.length > 0) {
|
30 | result = mergeTwoObjects(result, args.shift(), 0, objects);
|
31 | }
|
32 | return result;
|
33 | }
|
34 | exports.merge = merge;
|
35 | function takeValue(value) {
|
36 | if (isArray(value)) {
|
37 | return value.slice();
|
38 | }
|
39 | return value;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function mergeTwoObjects(one, two, level = 0, objects) {
|
50 | let result;
|
51 | if (level > MAX_LEVEL) {
|
52 | return undefined;
|
53 | }
|
54 | level++;
|
55 | if (isPrimitive(one) || isPrimitive(two) || isFunction(two)) {
|
56 | result = takeValue(two);
|
57 | }
|
58 | else if (isArray(one)) {
|
59 | result = one.slice();
|
60 | if (isArray(two)) {
|
61 | for (let i = 0, j = two.length; i < j; i++) {
|
62 | result.push(takeValue(two[i]));
|
63 | }
|
64 | }
|
65 | else if (isObject(two)) {
|
66 | const keys = Object.keys(two);
|
67 | for (let i = 0, j = keys.length; i < j; i++) {
|
68 | const key = keys[i];
|
69 | result[key] = takeValue(two[key]);
|
70 | }
|
71 | }
|
72 | }
|
73 | else if (isObject(one)) {
|
74 | if (isObject(two)) {
|
75 | if (!shouldMerge(one, two)) {
|
76 | return two;
|
77 | }
|
78 | result = Object.assign({}, one);
|
79 | const keys = Object.keys(two);
|
80 | for (let i = 0, j = keys.length; i < j; i++) {
|
81 | const key = keys[i];
|
82 | const twoValue = two[key];
|
83 | if (isPrimitive(twoValue)) {
|
84 | if (typeof twoValue === 'undefined') {
|
85 | delete result[key];
|
86 | }
|
87 | else {
|
88 |
|
89 | result[key] = twoValue;
|
90 | }
|
91 | }
|
92 | else {
|
93 | const obj1 = result[key];
|
94 | const obj2 = twoValue;
|
95 | if (wasObjectReferenced(one, key, objects) ||
|
96 | wasObjectReferenced(two, key, objects)) {
|
97 | delete result[key];
|
98 | }
|
99 | else {
|
100 | if (isObject(obj1) && isObject(obj2)) {
|
101 | const arr1 = objects.get(obj1) || [];
|
102 | const arr2 = objects.get(obj2) || [];
|
103 | arr1.push({ obj: one, key });
|
104 | arr2.push({ obj: two, key });
|
105 | objects.set(obj1, arr1);
|
106 | objects.set(obj2, arr2);
|
107 | }
|
108 | result[key] = mergeTwoObjects(result[key], twoValue, level, objects);
|
109 | }
|
110 | }
|
111 | }
|
112 | }
|
113 | else {
|
114 | result = two;
|
115 | }
|
116 | }
|
117 | return result;
|
118 | }
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 | function wasObjectReferenced(obj, key, objects) {
|
126 | const arr = objects.get(obj[key]) || [];
|
127 | for (let i = 0, j = arr.length; i < j; i++) {
|
128 | const info = arr[i];
|
129 | if (info.key === key && info.obj === obj) {
|
130 | return true;
|
131 | }
|
132 | }
|
133 | return false;
|
134 | }
|
135 | function isArray(value) {
|
136 | return Array.isArray(value);
|
137 | }
|
138 | function isFunction(value) {
|
139 | return typeof value === 'function';
|
140 | }
|
141 | function isObject(value) {
|
142 | return (!isPrimitive(value) &&
|
143 | !isArray(value) &&
|
144 | !isFunction(value) &&
|
145 | typeof value === 'object');
|
146 | }
|
147 | function isPrimitive(value) {
|
148 | return (typeof value === 'string' ||
|
149 | typeof value === 'number' ||
|
150 | typeof value === 'boolean' ||
|
151 | typeof value === 'undefined' ||
|
152 | value instanceof Date ||
|
153 | value instanceof RegExp ||
|
154 | value === null);
|
155 | }
|
156 | function shouldMerge(one, two) {
|
157 | if (!(0, lodash_merge_1.isPlainObject)(one) || !(0, lodash_merge_1.isPlainObject)(two)) {
|
158 | return false;
|
159 | }
|
160 | return true;
|
161 | }
|
162 |
|
\ | No newline at end of file |