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

Function equal

assert/equal.ts:112–224  ·  view source on GitHub ↗
(a: unknown, b: unknown)

Source from the content-addressed store, hash-verified

110 * ```
111 */
112export function equal(a: unknown, b: unknown): boolean {
113 const seen = new Map<unknown, unknown>();
114 return (function compare(a: unknown, b: unknown): boolean {
115 if (sameValueZero(a, b)) return true;
116 if (isPrimitive(a) || isPrimitive(b)) return false;
117
118 if (a instanceof Date && b instanceof Date) {
119 return Object.is(a.getTime(), b.getTime());
120 }
121 if (a && typeof a === "object" && b && typeof b === "object") {
122 if (!prototypesEqual(a, b)) {
123 return false;
124 }
125 if (a instanceof TypedArray) {
126 return compareTypedArrays(a as TypedArray, b as TypedArray);
127 }
128 if (
129 a instanceof ArrayBuffer ||
130 (globalThis.SharedArrayBuffer && a instanceof SharedArrayBuffer)
131 ) {
132 return compareTypedArrays(
133 new Uint8Array(a),
134 new Uint8Array(b as ArrayBuffer | SharedArrayBuffer),
135 );
136 }
137 if (a instanceof WeakMap) {
138 throw new TypeError("Cannot compare WeakMap instances");
139 }
140 if (a instanceof WeakSet) {
141 throw new TypeError("Cannot compare WeakSet instances");
142 }
143 if (a instanceof WeakRef) {
144 return compare(a.deref(), (b as WeakRef<WeakKey>).deref());
145 }
146 if (seen.get(a) === b) {
147 return true;
148 }
149 if (Object.keys(a).length !== Object.keys(b).length) {
150 return false;
151 }
152 seen.set(a, b);
153 if (isKeyedCollection(a) && isKeyedCollection(b)) {
154 if (a.size !== b.size) {
155 return false;
156 }
157
158 const aKeys = [...a.keys()];
159 const primitiveKeysFastPath = aKeys.every(isPrimitive);
160 if (primitiveKeysFastPath) {
161 if (a instanceof Set) {
162 return a.symmetricDifference(b).size === 0;
163 }
164
165 for (const key of aKeys) {
166 if (
167 !b.has(key) ||
168 !compare(a.get(key), (b as Map<unknown, unknown>).get(key))
169 ) {

Callers 6

equal_test.tsFile · 0.90
assertEqualsFunction · 0.90
assertNotEqualsFunction · 0.90
assertArrayIncludesFunction · 0.90
assertInlineSnapshotFunction · 0.90
assertSnapshotFunction · 0.90

Calls 14

prototypesEqualFunction · 0.85
compareTypedArraysFunction · 0.85
compareFunction · 0.85
isBasicObjectOrArrayFunction · 0.85
ownKeysFunction · 0.85
getKeysDeepFunction · 0.85
keysMethod · 0.80
sameValueZeroFunction · 0.70
isPrimitiveFunction · 0.70
isKeyedCollectionFunction · 0.70
getMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected