root / HServer / 00.Server / 00.Program / node_modules / lodash / toNumber.js
이력 | 보기 | 이력해설 | 다운로드 (1.54 KB)
1 |
var isObject = require('./isObject'), |
---|---|
2 |
isSymbol = require('./isSymbol');
|
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 `root`. */
|
20 |
var 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 |
*/
|
45 |
function 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 |
|
66 |
module.exports = toNumber; |