mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-09 10:27:49 +00:00
Ensure _.toNumber works with symbol objects without a valueOf method.
This commit is contained in:
@@ -11022,14 +11022,15 @@
|
|||||||
* // => 3
|
* // => 3
|
||||||
*/
|
*/
|
||||||
function toNumber(value) {
|
function toNumber(value) {
|
||||||
|
if (isSymbol(value)) {
|
||||||
|
return NAN;
|
||||||
|
}
|
||||||
if (isObject(value)) {
|
if (isObject(value)) {
|
||||||
var other = isFunction(value.valueOf) ? value.valueOf() : value;
|
var other = isFunction(value.valueOf) ? value.valueOf() : value;
|
||||||
value = isObject(other) ? (other + '') : other;
|
value = isObject(other) ? (other + '') : other;
|
||||||
}
|
}
|
||||||
if (typeof value != 'string') {
|
if (typeof value != 'string') {
|
||||||
return value === 0
|
return value === 0 ? value : +value;
|
||||||
? value
|
|
||||||
: (typeof value == 'symbol' ? NAN : +value);
|
|
||||||
}
|
}
|
||||||
value = value.replace(reTrim, '');
|
value = value.replace(reTrim, '');
|
||||||
var isBinary = reIsBinary.test(value);
|
var isBinary = reIsBinary.test(value);
|
||||||
|
|||||||
10
test/test.js
10
test/test.js
@@ -21990,7 +21990,15 @@
|
|||||||
assert.expect(1);
|
assert.expect(1);
|
||||||
|
|
||||||
if (Symbol) {
|
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 {
|
else {
|
||||||
skipAssert(assert);
|
skipAssert(assert);
|
||||||
|
|||||||
Reference in New Issue
Block a user