Skip to content

Commit

Permalink
assert,util: fix sparse array comparison
Browse files Browse the repository at this point in the history
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: nodejs#24749
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
BridgeAR authored and rvagg committed Feb 28, 2019
1 parent 9da4af7 commit 30aa8b2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/internal/util/comparisons.js
Expand Up @@ -543,11 +543,10 @@ function objEquiv(a, b, strict, keys, memos, iterationType) {
} else {
// Array is sparse.
const keysA = objectKeys(a);
i++;
for (; i < keysA.length; i++) {
const key = keysA[i];
if (!hasOwnProperty(b, key) ||
!innerDeepEqual(a[key], b[i], strict, memos)) {
!innerDeepEqual(a[key], b[key], strict, memos)) {
return false;
}
}
Expand Down
17 changes: 14 additions & 3 deletions test/parallel/test-assert-deep.js
Expand Up @@ -557,9 +557,20 @@ assertNotDeepOrStrict(
assertDeepAndStrictEqual(m3, m4);
}

// Handle sparse arrays
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
// Handle sparse arrays.
{
assertDeepAndStrictEqual([1, , , 3], [1, , , 3]);
assertOnlyDeepEqual([1, , , 3], [1, , , 3, , , ]);
const a = new Array(3);
const b = new Array(3);
a[2] = true;
b[1] = true;
assertNotDeepOrStrict(a, b);
b[2] = true;
assertNotDeepOrStrict(a, b);
a[0] = true;
assertNotDeepOrStrict(a, b);
}

// Handle different error messages
{
Expand Down

0 comments on commit 30aa8b2

Please sign in to comment.