(a: unknown, b: unknown)
| 110 | * ``` |
| 111 | */ |
| 112 | export 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 | ) { |
no test coverage detected