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.getDeepUnequalKeyValues = exports.deepCompareKeys = exports.shallowCompareKeys = exports.arraysEqual = void 0;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 | function arraysEqual(arrA, arrB, compare) {
|
26 | if (compare === void 0) { compare = function (a, b) { return a === b; }; }
|
27 |
|
28 | if (arrA == null && arrB == null) {
|
29 | return true;
|
30 | }
|
31 | else if (arrA == null || arrB == null || arrA.length !== arrB.length) {
|
32 | return false;
|
33 | }
|
34 | else {
|
35 | return arrA.every(function (a, i) { return compare(a, arrB[i]); });
|
36 | }
|
37 | }
|
38 | exports.arraysEqual = arraysEqual;
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function shallowCompareKeys(objA, objB, keys) {
|
46 |
|
47 | if (objA == null && objB == null) {
|
48 | return true;
|
49 | }
|
50 | else if (objA == null || objB == null) {
|
51 | return false;
|
52 | }
|
53 | else if (Array.isArray(objA) || Array.isArray(objB)) {
|
54 | return false;
|
55 | }
|
56 | else if (keys != null) {
|
57 | return shallowCompareKeysImpl(objA, objB, keys);
|
58 | }
|
59 | else {
|
60 |
|
61 | var keysA = Object.keys(objA);
|
62 | var keysB = Object.keys(objB);
|
63 | return (shallowCompareKeysImpl(objA, objB, { include: keysA }) &&
|
64 | shallowCompareKeysImpl(objA, objB, { include: keysB }));
|
65 | }
|
66 | }
|
67 | exports.shallowCompareKeys = shallowCompareKeys;
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 | function deepCompareKeys(objA, objB, keys) {
|
75 | if (objA === objB) {
|
76 | return true;
|
77 | }
|
78 | else if (objA == null && objB == null) {
|
79 |
|
80 | return true;
|
81 | }
|
82 | else if (objA == null || objB == null) {
|
83 | return false;
|
84 | }
|
85 | else if (Array.isArray(objA) || Array.isArray(objB)) {
|
86 | return arraysEqual(objA, objB, deepCompareKeys);
|
87 | }
|
88 | else if (isSimplePrimitiveType(objA) || isSimplePrimitiveType(objB)) {
|
89 | return objA === objB;
|
90 | }
|
91 | else if (keys != null) {
|
92 | return deepCompareKeysImpl(objA, objB, keys);
|
93 | }
|
94 | else if (objA.constructor !== objB.constructor) {
|
95 | return false;
|
96 | }
|
97 | else {
|
98 | var keysA = Object.keys(objA);
|
99 | var keysB = Object.keys(objB);
|
100 | if (keysA == null || keysB == null) {
|
101 | return false;
|
102 | }
|
103 | if (keysA.length === 0 && keysB.length === 0) {
|
104 | return true;
|
105 | }
|
106 | return arraysEqual(keysA, keysB) && deepCompareKeysImpl(objA, objB, keysA);
|
107 | }
|
108 | }
|
109 | exports.deepCompareKeys = deepCompareKeys;
|
110 |
|
111 |
|
112 |
|
113 |
|
114 | function getDeepUnequalKeyValues(objA, objB, keys) {
|
115 | if (objA === void 0) { objA = {}; }
|
116 | if (objB === void 0) { objB = {}; }
|
117 | var filteredKeys = keys == null ? unionKeys(objA, objB) : keys;
|
118 | return getUnequalKeyValues(objA, objB, filteredKeys, function (a, b, key) {
|
119 | return deepCompareKeys(a, b, [key]);
|
120 | });
|
121 | }
|
122 | exports.getDeepUnequalKeyValues = getDeepUnequalKeyValues;
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | function shallowCompareKeysImpl(objA, objB, keys) {
|
129 | return filterKeys(objA, objB, keys).every(function (key) {
|
130 | return objA.hasOwnProperty(key) === objB.hasOwnProperty(key) && objA[key] === objB[key];
|
131 | });
|
132 | }
|
133 |
|
134 |
|
135 |
|
136 | function deepCompareKeysImpl(objA, objB, keys) {
|
137 | return keys.every(function (key) {
|
138 | return objA.hasOwnProperty(key) === objB.hasOwnProperty(key) && deepCompareKeys(objA[key], objB[key]);
|
139 | });
|
140 | }
|
141 | function isSimplePrimitiveType(value) {
|
142 | return typeof value === "number" || typeof value === "string" || typeof value === "boolean";
|
143 | }
|
144 | function filterKeys(objA, objB, keys) {
|
145 | if (isAllowlist(keys)) {
|
146 | return keys.include;
|
147 | }
|
148 | else if (isDenylist(keys)) {
|
149 | var keysA = Object.keys(objA);
|
150 | var keysB = Object.keys(objB);
|
151 |
|
152 | var keySet_1 = arrayToObject(keysA.concat(keysB));
|
153 |
|
154 | keys.exclude.forEach(function (key) { return delete keySet_1[key]; });
|
155 |
|
156 | return Object.keys(keySet_1);
|
157 | }
|
158 | return [];
|
159 | }
|
160 | function isAllowlist(keys) {
|
161 | return keys != null && keys.include != null;
|
162 | }
|
163 | function isDenylist(keys) {
|
164 | return keys != null && keys.exclude != null;
|
165 | }
|
166 | function arrayToObject(arr) {
|
167 | return arr.reduce(function (obj, element) {
|
168 | obj[element] = true;
|
169 | return obj;
|
170 | }, {});
|
171 | }
|
172 | function getUnequalKeyValues(objA, objB, keys, compareFn) {
|
173 | var unequalKeys = keys.filter(function (key) { return !compareFn(objA, objB, key); });
|
174 | var unequalKeyValues = unequalKeys.map(function (key) { return ({
|
175 | key: key,
|
176 | valueA: objA[key],
|
177 | valueB: objB[key],
|
178 | }); });
|
179 | return unequalKeyValues;
|
180 | }
|
181 | function unionKeys(objA, objB) {
|
182 | var keysA = Object.keys(objA);
|
183 | var keysB = Object.keys(objB);
|
184 | var concatKeys = keysA.concat(keysB);
|
185 | var keySet = arrayToObject(concatKeys);
|
186 | return Object.keys(keySet);
|
187 | }
|
188 |
|
\ | No newline at end of file |