426fa15166efcbf056a9afc3f13b76ebfe65caca
[yaffs-website] / node_modules / uncss / node_modules / lodash / toNumber.js
1 var isFunction = require('./isFunction'),
2     isObject = require('./isObject');
3
4 /** Used as references for various `Number` constants. */
5 var NAN = 0 / 0;
6
7 /** Used to match leading and trailing whitespace. */
8 var reTrim = /^\s+|\s+$/g;
9
10 /** Used to detect bad signed hexadecimal string values. */
11 var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
12
13 /** Used to detect binary string values. */
14 var reIsBinary = /^0b[01]+$/i;
15
16 /** Used to detect octal string values. */
17 var reIsOctal = /^0o[0-7]+$/i;
18
19 /** Built-in method references without a dependency on `global`. */
20 var freeParseInt = parseInt;
21
22 /**
23  * Converts `value` to a number.
24  *
25  * @static
26  * @memberOf _
27  * @category Lang
28  * @param {*} value The value to process.
29  * @returns {number} Returns the number.
30  * @example
31  *
32  * _.toNumber(3);
33  * // => 3
34  *
35  * _.toNumber(Number.MIN_VALUE);
36  * // => 5e-324
37  *
38  * _.toNumber(Infinity);
39  * // => Infinity
40  *
41  * _.toNumber('3');
42  * // => 3
43  */
44 function toNumber(value) {
45   if (isObject(value)) {
46     var other = isFunction(value.valueOf) ? value.valueOf() : value;
47     value = isObject(other) ? (other + '') : other;
48   }
49   if (typeof value != 'string') {
50     return value === 0 ? value : +value;
51   }
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);
57 }
58
59 module.exports = toNumber;