Skip to content

Commit e95f7bd

Browse files
committed
Take inspiration from fast-deep-equal when comparing Set and Map
1 parent fbee2e7 commit e95f7bd

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/lib/customDeepEqual.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import deepEqual from 'fast-deep-equal/es6';
21
/**
32
* Compares two variables for deep equality.
43
* Gracefully handles equality checks on MediaStreamTracks by comparing their ids.
@@ -33,14 +32,36 @@ export function customDeepEqual(a: any, b: any): boolean {
3332
return a.source === b.source && a.flags === b.flags;
3433
}
3534

36-
// Handle special case for Set - use fast-deep-equal for this
35+
// Handle special case for Set
3736
if (a instanceof Set && b instanceof Set) {
38-
return deepEqual(a, b);
37+
if (a.size !== b.size) {
38+
return false;
39+
}
40+
41+
for (const value of a.values()) {
42+
if (!b.has(value)) {
43+
return false;
44+
}
45+
}
46+
47+
return true;
3948
}
4049

41-
// Handle special case for Map - use fast-deep-equal for this
50+
// Handle special case for Map
4251
if (a instanceof Map && b instanceof Map) {
43-
return deepEqual(a, b);
52+
if (a.size !== b.size) {
53+
return false;
54+
}
55+
for (const [key, value] of a.entries()) {
56+
if (!b.has(key)) {
57+
return false;
58+
}
59+
if (!customDeepEqual(value, b.get(key))) {
60+
return false;
61+
}
62+
}
63+
64+
return true;
4465
}
4566

4667
// If a or b are not objects or null, they can't be deeply equal

0 commit comments

Comments
 (0)