PageRenderTime 11ms CodeModel.GetById 7ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/lodash/toNumber.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 66 lines | 29 code | 8 blank | 29 comment | 8 complexity | c9deaf168b8f0a1bc2570e92d7e04e39 MD5 | raw file
 1var isObject = require('./isObject'),
 2    isSymbol = require('./isSymbol');
 3
 4/** Used as references for various `Number` constants. */
 5var NAN = 0 / 0;
 6
 7/** Used to match leading and trailing whitespace. */
 8var reTrim = /^\s+|\s+$/g;
 9
10/** Used to detect bad signed hexadecimal string values. */
11var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
12
13/** Used to detect binary string values. */
14var reIsBinary = /^0b[01]+$/i;
15
16/** Used to detect octal string values. */
17var reIsOctal = /^0o[0-7]+$/i;
18
19/** Built-in method references without a dependency on `root`. */
20var freeParseInt = parseInt;
21
22/**
23 * Converts `value` to a number.
24 *
25 * @static
26 * @memberOf _
27 * @since 4.0.0
28 * @category Lang
29 * @param {*} value The value to process.
30 * @returns {number} Returns the number.
31 * @example
32 *
33 * _.toNumber(3.2);
34 * // => 3.2
35 *
36 * _.toNumber(Number.MIN_VALUE);
37 * // => 5e-324
38 *
39 * _.toNumber(Infinity);
40 * // => Infinity
41 *
42 * _.toNumber('3.2');
43 * // => 3.2
44 */
45function toNumber(value) {
46  if (typeof value == 'number') {
47    return value;
48  }
49  if (isSymbol(value)) {
50    return NAN;
51  }
52  if (isObject(value)) {
53    var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
54    value = isObject(other) ? (other + '') : other;
55  }
56  if (typeof value != 'string') {
57    return value === 0 ? value : +value;
58  }
59  value = value.replace(reTrim, '');
60  var isBinary = reIsBinary.test(value);
61  return (isBinary || reIsOctal.test(value))
62    ? freeParseInt(value.slice(2), isBinary ? 2 : 8)
63    : (reIsBadHex.test(value) ? NAN : +value);
64}
65
66module.exports = toNumber;