MCPcopy Create free account
hub / github.com/RobPruzan/react-scratch / deepEqual

Function deepEqual

src/utils.ts:2–39  ·  view source on GitHub ↗
(a: any, b: any)

Source from the content-addressed store, hash-verified

1export const run = <T>(f: () => T) => f();
2export const deepEqual = (a: any, b: any): boolean => {
3 if (a === b) return true;
4
5 if (a && b && typeof a === "object" && typeof b === "object") {
6 if (Array.isArray(a) && Array.isArray(b)) {
7 if (a.length !== b.length) return false;
8 for (let i = 0; i < a.length; i++) {
9 if (!deepEqual(a[i], b[i])) {
10 return false;
11 }
12 }
13 return true;
14 }
15
16 if (a.constructor !== b.constructor) {
17 return false;
18 }
19
20 const keysA = Object.keys(a);
21 const keysB = Object.keys(b);
22
23 if (keysA.length !== keysB.length) {
24 return false;
25 }
26
27 for (const key of keysA) {
28 if (!keysB.includes(key)) {
29 return false;
30 }
31 if (!deepEqual(a[key], b[key])) {
32 return false;
33 }
34 }
35
36 return true;
37 }
38 return false;
39};
40
41export const deepCloneTree = <T>(obj: T, seen = new WeakSet()): T => {
42 if (obj === null || typeof obj !== "object") {

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected