MCPcopy
hub / github.com/jestjs/jest / eq

Function eq

packages/expect-utils/src/jasmineUtils.ts:63–207  ·  view source on GitHub ↗
(
  a: any,
  b: any,
  aStack: Array<unknown>,
  bStack: Array<unknown>,
  customTesters: Array<Tester>,
  strictCheck: boolean | undefined,
)

Source from the content-addressed store, hash-verified

61// Equality function lovingly adapted from isEqual in
62// [Underscore](http://underscorejs.org)
63function eq(
64 a: any,
65 b: any,
66 aStack: Array<unknown>,
67 bStack: Array<unknown>,
68 customTesters: Array<Tester>,
69 strictCheck: boolean | undefined,
70): boolean {
71 let result = true;
72
73 const asymmetricResult = asymmetricMatch(a, b);
74 if (asymmetricResult !== undefined) {
75 return asymmetricResult;
76 }
77
78 const testerContext: TesterContext = {equals};
79 for (const item of customTesters) {
80 const customTesterResult = item.call(testerContext, a, b, customTesters);
81 if (customTesterResult !== undefined) {
82 return customTesterResult;
83 }
84 }
85
86 if (a instanceof Error && b instanceof Error) {
87 return a.message === b.message;
88 }
89
90 if (Object.is(a, b)) {
91 return true;
92 }
93 // A strict comparison is necessary because `null == undefined`.
94 if (a === null || b === null) {
95 return false;
96 }
97 const className = Object.prototype.toString.call(a);
98 if (className !== Object.prototype.toString.call(b)) {
99 return false;
100 }
101 switch (className) {
102 case '[object Boolean]':
103 case '[object String]':
104 case '[object Number]':
105 if (typeof a !== typeof b) {
106 // One is a primitive, one a `new Primitive()`
107 return false;
108 } else if (typeof a !== 'object' && typeof b !== 'object') {
109 // both are proper primitives
110 return false;
111 } else {
112 // both are `new Primitive()`s
113 return Object.is(a.valueOf(), b.valueOf());
114 }
115 case '[object Date]':
116 // Coerce dates to numeric primitive values. Dates are compared by their
117 // millisecond representations. Note that invalid dates with millisecond representations
118 // of `NaN` are not equivalent.
119 return +a === +b;
120 // RegExps are compared by their source patterns and flags.

Callers 1

equalsFunction · 0.85

Calls 5

isDomNodeFunction · 0.85
keysFunction · 0.85
isAsymmetricFunction · 0.85
hasKeyFunction · 0.85
asymmetricMatchFunction · 0.70

Tested by

no test coverage detected