Skip to content

Commit 5ee9762

Browse files
committed
frontend: fix handling of sparse arrays in equal function
Fixed failing test introduced in last commit and correctly handle sparse arrays.
1 parent 3eaa089 commit 5ee9762

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

frontends/web/src/utils/equal.test.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ describe('equal', () => {
8787
});
8888

8989
it('compares sparse array vs defined array', () => {
90+
/*eslint no-sparse-arrays: "off"*/
9091
expect(equal([1, , 3], [1, undefined, 3])).toBeFalsy();
92+
/*eslint no-sparse-arrays: "off"*/
9193
expect(equal([1, , 3], [1, , 3])).toBeTruthy();
9294
});
9395
});
@@ -214,4 +216,4 @@ describe('edge cases: array vs object structure', () => {
214216
it('nested empty object vs array is not equal', () => {
215217
expect(equal({ foo: [] }, { foo: {} })).toBeFalsy();
216218
});
217-
});
219+
});

frontends/web/src/utils/equal.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Copyright 2018 Shift Devices AG
3+
* Copyright 2025 Shift Crypto AG
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
56
* you may not use this file except in compliance with the License.
@@ -61,7 +62,12 @@ export const equal = (a: unknown, b: unknown): boolean => {
6162
return false;
6263
}
6364
for (let i = 0; i < a.length; i++) {
64-
if (!equal(a[i], b[i])) {
65+
// handle sparse arrays
66+
const hasA = i in a;
67+
if (hasA !== i in b) {
68+
return false;
69+
}
70+
if (hasA && !equal(a[i], b[i])) {
6571
return false;
6672
}
6773
}

0 commit comments

Comments
 (0)