1 var baseTrim = require('./_baseTrim'),
2 isObject = require('./isObject'),
3 isSymbol = require('./isSymbol');
5 /** Used as references for various `Number` constants. */
8 /** Used to detect bad signed hexadecimal string values. */
9 var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
11 /** Used to detect binary string values. */
12 var reIsBinary = /^0b[01]+$/i;
14 /** Used to detect octal string values. */
15 var reIsOctal = /^0o[0-7]+$/i;
17 /** Built-in method references without a dependency on `root`. */
18 var freeParseInt = parseInt;
21 * Converts `value` to a number.
27 * @param {*} value The value to process.
28 * @returns {number} Returns the number.
34 * _.toNumber(Number.MIN_VALUE);
37 * _.toNumber(Infinity);
43 function toNumber(value) {
44 if (typeof value == 'number') {
47 if (isSymbol(value)) {
50 if (isObject(value)) {
51 var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
52 value = isObject(other) ? (other + '') : other;
54 if (typeof value != 'string') {
55 return value === 0 ? value : +value;
57 value = baseTrim(value);
58 var isBinary = reIsBinary.test(value);
59 return (isBinary || reIsOctal.test(value))
60 ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
61 : (reIsBadHex.test(value) ? NAN : +value);
64 module.exports = toNumber;