MCPcopy Create free account
hub / github.com/denoland/std / equal

Function equal

expect/_equal.ts:42–230  ·  view source on GitHub ↗
(c: unknown, d: unknown, options?: EqualOptions)

Source from the content-addressed store, hash-verified

40 * @param options for the equality check
41 */
42export function equal(c: unknown, d: unknown, options?: EqualOptions): boolean {
43 const { customTesters = [], strictCheck } = options ?? {};
44 const seen = new Map();
45
46 return (function compare(a: unknown, b: unknown): boolean {
47 const asymmetric = asymmetricEqual(a, b);
48 if (asymmetric !== undefined) {
49 return asymmetric;
50 }
51
52 if (customTesters?.length) {
53 for (const customTester of customTesters) {
54 const testContext = {
55 equal,
56 };
57 const pass = customTester.call(testContext, a, b, customTesters);
58 if (pass !== undefined) {
59 return pass;
60 }
61 }
62 }
63
64 // Have to render RegExp & Date for string comparison
65 // unless it's mistreated as object
66 if (
67 a &&
68 b &&
69 ((a instanceof RegExp && b instanceof RegExp) ||
70 (a instanceof URL && b instanceof URL))
71 ) {
72 return String(a) === String(b);
73 }
74
75 if (a instanceof Date && b instanceof Date) {
76 const aTime = a.getTime();
77 const bTime = b.getTime();
78 // Check for NaN equality manually since NaN is not
79 // equal to itself.
80 if (Number.isNaN(aTime) && Number.isNaN(bTime)) {
81 return true;
82 }
83 return aTime === bTime;
84 }
85 if (a instanceof Error && b instanceof Error) {
86 return a.message === b.message;
87 }
88 if (typeof a === "number" && typeof b === "number") {
89 return Number.isNaN(a) && Number.isNaN(b) || a === b;
90 }
91 if (a === null || b === null) {
92 return a === b;
93 }
94 const className = Object.prototype.toString.call(a);
95 if (className !== Object.prototype.toString.call(b)) {
96 return false;
97 }
98 if (Object.is(a, b)) {
99 return true;

Callers 15

toHavePropertyFunction · 0.90
toContainEqualFunction · 0.90
toMatchObjectFunction · 0.90
toHaveBeenCalledWithFunction · 0.90
toHaveBeenLastCalledWithFunction · 0.90
toHaveBeenNthCalledWithFunction · 0.90
toHaveReturnedWithFunction · 0.90
toHaveLastReturnedWithFunction · 0.90
toHaveNthReturnedWithFunction · 0.90
applyPropertyMatchersFunction · 0.90
assertEqualsFunction · 0.90
toHaveReturnedWithFunction · 0.90

Calls 9

asymmetricEqualFunction · 0.85
constructorsEqualFunction · 0.85
compareFunction · 0.85
keysMethod · 0.80
isKeyedCollectionFunction · 0.70
getMethod · 0.65
setMethod · 0.65
hasMethod · 0.65
entriesMethod · 0.45

Tested by

no test coverage detected