MCPcopy
hub / github.com/facebook/react / shallowEqual

Function shallowEqual

packages/shared/shallowEqual.js:18–52  ·  view source on GitHub ↗

* Performs equality by iterating through keys on an object and returning false * when any key has values which are not strictly equal between the arguments. * Returns true when the values of all keys are strictly equal.

(objA: mixed, objB: mixed)

Source from the content-addressed store, hash-verified

16 * Returns true when the values of all keys are strictly equal.
17 */
18function shallowEqual(objA: mixed, objB: mixed): boolean {
19 if (is(objA, objB)) {
20 return true;
21 }
22
23 if (
24 typeof objA !== 'object' ||
25 objA === null ||
26 typeof objB !== 'object' ||
27 objB === null
28 ) {
29 return false;
30 }
31
32 const keysA = Object.keys(objA);
33 const keysB = Object.keys(objB);
34
35 if (keysA.length !== keysB.length) {
36 return false;
37 }
38
39 // Test for A's keys different from B.
40 for (let i = 0; i < keysA.length; i++) {
41 const currentKey = keysA[i];
42 if (
43 !hasOwnProperty.call(objB, currentKey) ||
44 // $FlowFixMe[incompatible-use] lost refinement of `objB`
45 !is(objA[currentKey], objB[currentKey])
46 ) {
47 return false;
48 }
49 }
50
51 return true;
52}
53
54export default shallowEqual;

Callers

nothing calls this directly

Calls 2

isFunction · 0.85
keysMethod · 0.80

Tested by

no test coverage detected