MessagesForMacintosh/JS/node_modules/@wry/equality/lib/equality.esm.js.map

1 line
7.0 KiB
Plaintext
Raw Normal View History

{"version":3,"file":"equality.esm.js","sources":["../src/equality.ts"],"sourcesContent":["const { toString, hasOwnProperty } = Object.prototype;\nconst previousComparisons = new Map<object, Set<object>>();\n\n/**\n * Performs a deep equality check on two JavaScript values, tolerating cycles.\n */\nexport function equal(a: any, b: any): boolean {\n try {\n return check(a, b);\n } finally {\n previousComparisons.clear();\n }\n}\n\n// Allow default imports as well.\nexport default equal;\n\nfunction check(a: any, b: any): boolean {\n // If the two values are strictly equal, our job is easy.\n if (a === b) {\n return true;\n }\n\n // Object.prototype.toString returns a representation of the runtime type of\n // the given value that is considerably more precise than typeof.\n const aTag = toString.call(a);\n const bTag = toString.call(b);\n\n // If the runtime types of a and b are different, they could maybe be equal\n // under some interpretation of equality, but for simplicity and performance\n // we just return false instead.\n if (aTag !== bTag) {\n return false;\n }\n\n switch (aTag) {\n case '[object Array]':\n // Arrays are a lot like other objects, but we can cheaply compare their\n // lengths as a short-cut before comparing their elements.\n if (a.length !== b.length) return false;\n // Fall through to object case...\n case '[object Object]': {\n if (previouslyCompared(a, b)) return true;\n\n const aKeys = Object.keys(a);\n const bKeys = Object.keys(b);\n\n // If `a` and `b` have a different number of enumerable keys, they\n // must be different.\n const keyCount = aKeys.length;\n if (keyCount !== bKeys.length) return false;\n\n // Now make sure they have the same keys.\n for (let k = 0; k < keyCount; ++k) {\n if (!hasOwnProperty.call(b, aKeys[k])) {\n return false;\n }\n }\n\n // Finally, check deep equality of all child properties.\n for (let k = 0; k < keyCount; ++k) {\n const key = aKeys[k];\n if (!check(a[key], b[key])) {\n return false;\n }\n }\n\n return true;\n }\n\n case '[object Error]':\n return a.name === b.name && a.message === b.message;\n\n case '[object Number]':\n // Handle NaN, which is !== itself.\n if (a !== a) return b !== b;\n // Fall through to shared +a === +b case...\n case '[object Boolean]':\n case '[object Date]':\n return +a === +b;\n\n case '[object RegExp]':\n case '[object String]':\n return a == `${b}`;\n\n case '[object Map]':\n case '[object Set]': {\n if (a.size !== b.size) return false;\n if (previouslyCompared(a, b)) return true;\n\n const aIterator = a.entries();\n const isMap = aTag === '[object Map]';\n\n while (true) {\n const info = aIterator.next();\n if (info.done) break;\n\n // If a instanceof Set, aValue === aKey.\n const [aKey, aValue] = info.value;\n\n // So this works the same way for both Set and Map.\n if (!b.has(aKey)) {\n return false;\n }\n\n // However, we care about deep equality of values only when dealing\n // with Map structures.\n if (isMap && !check(aValue, b.get(aKey))) {\n return false;\n }\n }\n\n return true;\n }\n }\n\n // Otherwise the values are not equal.\n return false;\n}\n\nfunction previouslyCompared(a: object, b: object): boolean {\n // Though cyclic references can make an object graph appear infinite from the\n // perspective of a depth-first traversal, the graph still contains a finite\n // number of distinct object references. We use the previousComparisons cache\n // to avoid comparing the same pair of object references more than once, which\n // guarantees termination (even if we end up comparing every object in one\n // graph to every object in the other graph, which is extremely unlikely),\n // while still allowing weird isomorphic structures (like rings with differ