Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | 2x 36x 60x 24x 36x 8x 5x 5x 2x 1x 28x 3x 25x 17x 2x 15x 15x 15x 2x 13x 13x 13x 2x 11x 11x 18x 5x 6x 8x 4x | // @flow
import type { Equals } from ".";
const hasOwn = (object, key) =>
Object.prototype.hasOwnProperty.call(object, key);
export default function deepEquals(equals: Equals, deepObjects: boolean) {
function deep(valueA: any, valueB: any) {
if (equals(valueA, valueB)) {
return true;
}
if (Array.isArray(valueA)) {
if (!Array.isArray(valueB) || valueA.length !== valueB.length) {
return false;
}
// Check deep equality of each value in A against the same indexed value in B
if (!valueA.every((value, index) => deep(value, valueB[index]))) {
return false;
}
// could not find unequal items
return true;
}
if (Array.isArray(valueB)) {
return false;
}
if (typeof valueA === "object") {
if (typeof valueB !== "object") {
return false;
}
const isANull = valueA === null;
const isBNull = valueB === null;
if (isANull || isBNull) {
return isANull === isBNull;
}
const aKeys = Object.keys(valueA);
const bKeys = Object.keys(valueB);
if (aKeys.length !== bKeys.length) {
return false;
}
// Should we compare with shallow equivalence or deep equivalence?
const equalityChecker = deepObjects ? deep : equals;
// Check if objects share same keys, and each of those keys are equal
if (
!aKeys.every(
aKey =>
hasOwn(valueA, aKey) &&
hasOwn(valueB, aKey) &&
equalityChecker(valueA[aKey], valueB[aKey])
)
) {
return false;
}
// could not find unequal keys or values
return true;
}
return false;
}
return deep;
}
|