-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix max stack errors on cycles.
Replaces fast-deep-equal with a copy/pasted/inlined version of a cycle-aware deepEquals.
- Loading branch information
Showing
5 changed files
with
64 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Our own `deepEquals` because either `fast-deep-equals` or `dequal` or etc actually | ||
* handle cyclic data structures, despite ChatGTP's assertions/hallucinations. | ||
* | ||
* Ported from https://github.com/KoryNunn/cyclic-deep-equal which is ISC. | ||
*/ | ||
export function deepEquals(a: any, b: any, visited: Set<any> = new Set()): boolean { | ||
const aType = typeof a; | ||
if (aType !== typeof b) return false; | ||
|
||
if (a == null || b == null || !(aType === "object" || aType === "function")) { | ||
if (aType === "number" && isNaN(a) && isNaN(b)) return true; | ||
return a === b; | ||
} | ||
|
||
if (Array.isArray(a) !== Array.isArray(b)) return false; | ||
|
||
const aKeys = Object.keys(a), | ||
bKeys = Object.keys(b); | ||
if (aKeys.length !== bKeys.length) return false; | ||
|
||
let equal = true; | ||
|
||
for (const key of aKeys) { | ||
if (!(key in b)) { | ||
equal = false; | ||
break; | ||
} | ||
if (a[key] && a[key] instanceof Object) { | ||
if (visited.has(a[key])) break; | ||
visited.add(a[key]); | ||
} | ||
if (!deepEquals(a[key], b[key], visited)) { | ||
equal = false; | ||
break; | ||
} | ||
} | ||
|
||
return equal; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters