UNPKG

6.58 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright 2017 Palantir Technologies, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.getDeepUnequalKeyValues = exports.deepCompareKeys = exports.shallowCompareKeys = exports.arraysEqual = void 0;
19/* eslint-enable deprecation/deprecation */
20/**
21 * Returns true if the arrays are equal. Elements will be shallowly compared by
22 * default, or they will be compared using the custom `compare` function if one
23 * is provided.
24 */
25function arraysEqual(arrA, arrB, compare) {
26 if (compare === void 0) { compare = function (a, b) { return a === b; }; }
27 // treat `null` and `undefined` as the same
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}
38exports.arraysEqual = arraysEqual;
39/**
40 * Shallow comparison between objects. If `keys` is provided, just that subset
41 * of keys will be compared; otherwise, all keys will be compared.
42 *
43 * @returns true if items are equal.
44 */
45function shallowCompareKeys(objA, objB, keys) {
46 // treat `null` and `undefined` as the same
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 // shallowly compare all keys from both objects
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}
67exports.shallowCompareKeys = shallowCompareKeys;
68/**
69 * Deep comparison between objects. If `keys` is provided, just that subset of
70 * keys will be compared; otherwise, all keys will be compared.
71 *
72 * @returns true if items are equal.
73 */
74function deepCompareKeys(objA, objB, keys) {
75 if (objA === objB) {
76 return true;
77 }
78 else if (objA == null && objB == null) {
79 // treat `null` and `undefined` as the same
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}
109exports.deepCompareKeys = deepCompareKeys;
110/**
111 * Returns a descriptive object for each key whose values are deeply unequal
112 * between two provided objects. Useful for debugging shouldComponentUpdate.
113 */
114function 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}
122exports.getDeepUnequalKeyValues = getDeepUnequalKeyValues;
123// Private helpers
124// ===============
125/**
126 * Partial shallow comparison between objects using the given list of keys.
127 */
128function 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 * Partial deep comparison between objects using the given list of keys.
135 */
136function 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}
141function isSimplePrimitiveType(value) {
142 return typeof value === "number" || typeof value === "string" || typeof value === "boolean";
143}
144function 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 // merge keys from both objects into a big set for quick access
152 var keySet_1 = arrayToObject(keysA.concat(keysB));
153 // delete denied keys from the key set
154 keys.exclude.forEach(function (key) { return delete keySet_1[key]; });
155 // return the remaining keys as an array
156 return Object.keys(keySet_1);
157 }
158 return [];
159}
160function isAllowlist(keys) {
161 return keys != null && keys.include != null;
162}
163function isDenylist(keys) {
164 return keys != null && keys.exclude != null;
165}
166function arrayToObject(arr) {
167 return arr.reduce(function (obj, element) {
168 obj[element] = true;
169 return obj;
170 }, {});
171}
172function 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}
181function 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//# sourceMappingURL=compareUtils.js.map
\No newline at end of file