1 | import baseTrim from './_baseTrim.js';
|
2 | import isObject from './isObject.js';
|
3 | import isSymbol from './isSymbol.js';
|
4 |
|
5 |
|
6 | var NAN = 0 / 0;
|
7 |
|
8 |
|
9 | var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
|
10 |
|
11 |
|
12 | var reIsBinary = /^0b[01]+$/i;
|
13 |
|
14 |
|
15 | var reIsOctal = /^0o[0-7]+$/i;
|
16 |
|
17 |
|
18 | var freeParseInt = parseInt;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 | function toNumber(value) {
|
44 | if (typeof value == 'number') {
|
45 | return value;
|
46 | }
|
47 | if (isSymbol(value)) {
|
48 | return NAN;
|
49 | }
|
50 | if (isObject(value)) {
|
51 | var other = typeof value.valueOf == 'function' ? value.valueOf() : value;
|
52 | value = isObject(other) ? (other + '') : other;
|
53 | }
|
54 | if (typeof value != 'string') {
|
55 | return value === 0 ? value : +value;
|
56 | }
|
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);
|
62 | }
|
63 |
|
64 | export default toNumber;
|