1 | /**
|
2 | * Copyright (c) 2013-present, Facebook, Inc.
|
3 | *
|
4 | * This source code is licensed under the MIT license found in the
|
5 | * LICENSE file in the root directory of this source tree.
|
6 | *
|
7 | * @typechecks
|
8 | *
|
9 | */
|
10 | /*eslint-disable no-self-compare */
|
11 | ;
|
12 | const hasOwnProperty = Object.prototype.hasOwnProperty;
|
13 | /**
|
14 | * inlined Object.is polyfill to avoid requiring consumers ship their own
|
15 | * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
|
16 | */
|
17 | const is = (x, y) => {
|
18 | // SameValue algorithm
|
19 | if (x === y) {
|
20 | // Steps 1-5, 7-10
|
21 | // Steps 6.b-6.e: +0 != -0
|
22 | // Added the nonzero y check to make Flow happy, but it is redundant
|
23 | return x !== 0 || y !== 0 || 1 / x === 1 / y;
|
24 | }
|
25 | else {
|
26 | // Step 6.a: NaN == NaN
|
27 | return x !== x && y !== y;
|
28 | }
|
29 | };
|
30 | /**
|
31 | * Performs equality by iterating through keys on an object and returning false
|
32 | * when any key has values which are not strictly equal between the arguments.
|
33 | * Returns true when the values of all keys are strictly equal.
|
34 | */
|
35 | export const shallowEqual = (objA, objB) => {
|
36 | if (is(objA, objB)) {
|
37 | return true;
|
38 | }
|
39 | if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
|
40 | return false;
|
41 | }
|
42 | var keysA = Object.keys(objA);
|
43 | var keysB = Object.keys(objB);
|
44 | if (keysA.length !== keysB.length) {
|
45 | return false;
|
46 | }
|
47 | // Test for A's keys different from B.
|
48 | for (var i = 0; i < keysA.length; i++) {
|
49 | if (!hasOwnProperty.call(objB, keysA[i]) || !is(objA[keysA[i]], objB[keysA[i]])) {
|
50 | return false;
|
51 | }
|
52 | }
|
53 | return true;
|
54 | };
|