Skip to content

Commit 5a81a4f

Browse files
BridgeARBethGriggs
authored andcommitted
assert,util: fix sparse array comparison
Comparing sparse arrays did not work properly. That is fixed and tests were added to verify that everything works as expected. This had an impact on `util.isDeepStrictEqual()` and `assert.deepStrictEqual()` and their counterpart `assert.notDeepStrictEqual()`. PR-URL: #24749 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Michaël Zasso <[email protected]>
1 parent 732088d commit 5a81a4f

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/internal/util/comparisons.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -543,11 +543,10 @@ function objEquiv(a, b, strict, keys, memos, iterationType) {
543543
} else {
544544
// Array is sparse.
545545
const keysA = objectKeys(a);
546-
i++;
547546
for (; i < keysA.length; i++) {
548547
const key = keysA[i];
549548
if (!hasOwnProperty(b, key) ||
550-
!innerDeepEqual(a[key], b[i], strict, memos)) {
549+
!innerDeepEqual(a[key], b[key], strict, memos)) {
551550
return false;
552551
}
553552
}

test/parallel/test-assert-deep.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -557,9 +557,20 @@ assertNotDeepOrStrict(
557557
assertDeepAndStrictEqual(m3, m4);
558558
}
559559

560-
// Handle sparse arrays
561-
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
562-
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
560+
// Handle sparse arrays.
561+
{
562+
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
563+
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
564+
const a = new Array(3);
565+
const b = new Array(3);
566+
a[2] = true;
567+
b[1] = true;
568+
assertNotDeepOrStrict(a, b);
569+
b[2] = true;
570+
assertNotDeepOrStrict(a, b);
571+
a[0] = true;
572+
assertNotDeepOrStrict(a, b);
573+
}
563574

564575
// Handle different error messages
565576
{

0 commit comments

Comments
 (0)