From 82830a809be726bfcfc62b57c203b7927aaac77e Mon Sep 17 00:00:00 2001 From: Ruben Bridgewater Date: Mon, 6 Aug 2018 01:19:01 +0200 Subject: [PATCH] assert: fix loose assert with map and set There was an oversight when checking the possible loose comparisons. This is now fixed by also accepting `''` instead of `0` or `false`. PR-URL: https://github.com/nodejs/node/pull/22145 Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat --- lib/internal/util/comparisons.js | 33 ++++++++++++++++++++----------- test/parallel/test-assert-deep.js | 12 +++++++---- 2 files changed, 29 insertions(+), 16 deletions(-) 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.