From 7eba5a22210bfc475c03ab0b837090875b90c814 Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Sat, 5 Mar 2016 16:16:49 -0800 Subject: [PATCH] Ensure `_.toNumber` works with symbol objects without a `valueOf` method. --- lodash.js | 7 ++++--- test/test.js | 10 +++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index dfe6e57ce..6db75064d 100644 --- a/lodash.js +++ b/lodash.js @@ -11022,14 +11022,15 @@ * // => 3 */ function toNumber(value) { + if (isSymbol(value)) { + return NAN; + } if (isObject(value)) { var other = isFunction(value.valueOf) ? value.valueOf() : value; value = isObject(other) ? (other + '') : other; } if (typeof value != 'string') { - return value === 0 - ? value - : (typeof value == 'symbol' ? NAN : +value); + return value === 0 ? value : +value; } value = value.replace(reTrim, ''); var isBinary = reIsBinary.test(value); diff --git a/test/test.js b/test/test.js index 45dced171..9ef599bf2 100644 --- a/test/test.js +++ b/test/test.js @@ -21990,7 +21990,15 @@ assert.expect(1); if (Symbol) { - assert.deepEqual(func(symbol), isToNumber ? NaN : 0); + var object1 = Object(symbol), + object2 = Object(symbol), + values = [symbol, object1, object2], + expected = lodashStable.map(values, lodashStable.constant(isToNumber ? NaN : 0)); + + object2.valueOf = undefined; + var actual = lodashStable.map(values, func); + + assert.deepEqual(actual, expected); } else { skipAssert(assert);