UNPKG

6.18 kBJavaScriptView Raw
1/*
2 * Copyright 2017 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/**
17 * Returns true if the arrays are equal. Elements will be shallowly compared by
18 * default, or they will be compared using the custom `compare` function if one
19 * is provided.
20 */
21export function arraysEqual(arrA, arrB, compare) {
22 if (compare === void 0) { compare = function (a, b) { return a === b; }; }
23 // treat `null` and `undefined` as the same
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 * Shallow comparison between objects. If `keys` is provided, just that subset
36 * of keys will be compared; otherwise, all keys will be compared.
37 *
38 * @returns true if items are equal.
39 */
40export function shallowCompareKeys(objA, objB, keys) {
41 // treat `null` and `undefined` as the same
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 // shallowly compare all keys from both objects
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 * Deep comparison between objects. If `keys` is provided, just that subset of
64 * keys will be compared; otherwise, all keys will be compared.
65 *
66 * @returns true if items are equal.
67 */
68export function deepCompareKeys(objA, objB, keys) {
69 if (objA === objB) {
70 return true;
71 }
72 else if (objA == null && objB == null) {
73 // treat `null` and `undefined` as the same
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 * Returns a descriptive object for each key whose values are deeply unequal
105 * between two provided objects. Useful for debugging shouldComponentUpdate.
106 */
107export 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// Private helpers
116// ===============
117/**
118 * Partial shallow comparison between objects using the given list of keys.
119 */
120function 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 * Partial deep comparison between objects using the given list of keys.
127 */
128function 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}
133function isSimplePrimitiveType(value) {
134 return typeof value === "number" || typeof value === "string" || typeof value === "boolean";
135}
136function 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 // merge keys from both objects into a big set for quick access
144 var keySet_1 = arrayToObject(keysA.concat(keysB));
145 // delete denied keys from the key set
146 keys.exclude.forEach(function (key) { return delete keySet_1[key]; });
147 // return the remaining keys as an array
148 return Object.keys(keySet_1);
149 }
150 return [];
151}
152function isAllowlist(keys) {
153 return keys != null && keys.include != null;
154}
155function isDenylist(keys) {
156 return keys != null && keys.exclude != null;
157}
158function arrayToObject(arr) {
159 return arr.reduce(function (obj, element) {
160 obj[element] = true;
161 return obj;
162 }, {});
163}
164function 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}
173function 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//# sourceMappingURL=compareUtils.js.map
\No newline at end of file