-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
50 lines (34 loc) · 973 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
44
45
46
47
48
49
50
'use strict';
var isNil = require('is-nil');
var isSymbol = require('is-symbol');
var isObject = require('is-object');
var isFunction = require('is-function');
var NAN = 0 / 0;
module.exports = function (value) {
if (isNil(value)) {
return 0;
}
if (typeof value === 'number') {
return value;
}
if (isSymbol(value)) {
return NAN;
}
if (isObject(value)) {
var raw = isFunction(value.valueOf) ? value.valueOf() : value;
value = isObject(raw) ? (raw + '') : raw;
}
var type = typeof value;
if (type !== 'string') {
return type === 'number' ? value : +value;
}
// trim
value = value.replace(/^\s+|\s+$/g, '');
var reIsBinary = /^0b[01]+$/i;
var reIsOctal = /^0o[0-7]+$/i;
var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
var isBinary = reIsBinary.test(value);
return (isBinary || reIsOctal.test(value))
? parseInt(value.slice(2), isBinary ? 2 : 8)
: (reIsBadHex.test(value) ? NAN : +value);
};