Skip to content

Commit

Permalink
assert: fix loose assert with map and set
Browse files Browse the repository at this point in the history
There was an oversight when checking the possible loose comparisons.
This is now fixed by also accepting `''` instead of `0` or `false`.

PR-URL: nodejs#22145
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
BridgeAR committed Aug 9, 2018
1 parent 1d859ef commit 85bfd71
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
33 changes: 21 additions & 12 deletions lib/internal/util/comparisons.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,29 +354,38 @@ function setEquiv(a, b, strict, memo) {
return true;
}

// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using
function findLooseMatchingPrimitives(prim) {
var values, number;
switch (typeof prim) {
case 'number':
values = ['' + prim];
if (prim === 1 || prim === 0)
values.push(Boolean(prim));
return values;
if (prim === 0) {
return ['', '0', false];
}
if (prim === 1) {
return ['1', true];
}
return ['' + prim];
case 'string':
number = +prim;
if (prim === '' || prim === '0') {
return [0, false];
}
if (prim === '1') {
return [1, true];
}
const number = +prim;
if ('' + number === prim) {
values = [number];
if (number === 1 || number === 0)
values.push(Boolean(number));
return [number];
}
return values;
return;
case 'undefined':
return [null];
case 'object': // Only pass in null as object!
return [undefined];
case 'boolean':
number = +prim;
return [number, '' + number];
if (prim === false) {
return ['', '0', 0];
}
return ['1', 1];
}
}

Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-assert-deep.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ assertDeepAndStrictEqual(
new Map([[1, undefined]])
);
assertOnlyDeepEqual(
new Map([[1, null]]),
new Map([['1', undefined]])
new Map([[1, null], ['', '0']]),
new Map([['1', undefined], [false, 0]])
);
assertNotDeepOrStrict(
new Map([[1, undefined]]),
Expand All @@ -373,8 +373,12 @@ assertOnlyDeepEqual(
new Map([[undefined, null]])
);
assertOnlyDeepEqual(
new Set([null]),
new Set([undefined])
new Set([null, '']),
new Set([undefined, 0])
);
assertNotDeepOrStrict(
new Set(['']),
new Set(['0'])
);

// GH-6416. Make sure circular refs don't throw.
Expand Down

0 comments on commit 85bfd71

Please sign in to comment.