diff --git a/lib/internal/util/comparisons.js b/lib/internal/util/comparisons.js index f4491f070f18d6..5b29ccf561c71b 100644 --- a/lib/internal/util/comparisons.js +++ b/lib/internal/util/comparisons.js @@ -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]; } } diff --git a/test/parallel/test-assert-deep.js b/test/parallel/test-assert-deep.js index 538f4bef49d8c6..431106c99be2b6 100644 --- a/test/parallel/test-assert-deep.js +++ b/test/parallel/test-assert-deep.js @@ -350,8 +350,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]]), @@ -368,8 +368,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.