1 var isFunction = require('./isFunction'),
2 isObject = require('./isObject');
4 /** Used as references for various `Number` constants. */
7 /** Used to match leading and trailing whitespace. */
8 var reTrim = /^\s+|\s+$/g;
10 /** Used to detect bad signed hexadecimal string values. */
11 var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
13 /** Used to detect binary string values. */
14 var reIsBinary = /^0b[01]+$/i;
16 /** Used to detect octal string values. */
17 var reIsOctal = /^0o[0-7]+$/i;
19 /** Built-in method references without a dependency on `root`. */
20 var freeParseInt = parseInt;
23 * Converts `value` to a number.
28 * @param {*} value The value to process.
29 * @returns {number} Returns the number.
35 * _.toNumber(Number.MIN_VALUE);
38 * _.toNumber(Infinity);
44 function toNumber(value) {
45 if (isObject(value)) {
46 var other = isFunction(value.valueOf) ? value.valueOf() : value;
47 value = isObject(other) ? (other + '') : other;
49 if (typeof value != 'string') {
50 return value === 0 ? value : +value;
52 value = value.replace(reTrim, '');
53 var isBinary = reIsBinary.test(value);
54 return (isBinary || reIsOctal.test(value))
55 ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
56 : (reIsBadHex.test(value) ? NAN : +value);
59 module.exports = toNumber;