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