artdaq_node_server  v1_00_08
 All Classes Namespaces Files Variables Pages
toNumber.js
1 var isObject = require('./isObject'),
2  isSymbol = require('./isSymbol');
3 
5 var NAN = 0 / 0;
6 
8 var reTrim = /^\s+|\s+$/g;
9 
11 var reIsBadHex = /^[-+]0x[0-9a-f]+$/i;
12 
14 var reIsBinary = /^0b[01]+$/i;
15 
17 var reIsOctal = /^0o[0-7]+$/i;
18 
20 var freeParseInt = parseInt;
21 
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;