00001 var isObject = require('./isObject'), 00002 isSymbol = require('./isSymbol'); 00003 00005 var NAN = 0 / 0; 00006 00008 var reTrim = /^\s+|\s+$/g; 00009 00011 var reIsBadHex = /^[-+]0x[0-9a-f]+$/i; 00012 00014 var reIsBinary = /^0b[01]+$/i; 00015 00017 var reIsOctal = /^0o[0-7]+$/i; 00018 00020 var freeParseInt = parseInt; 00021 00045 function toNumber(value) { 00046 if (typeof value == 'number') { 00047 return value; 00048 } 00049 if (isSymbol(value)) { 00050 return NAN; 00051 } 00052 if (isObject(value)) { 00053 var other = typeof value.valueOf == 'function' ? value.valueOf() : value; 00054 value = isObject(other) ? (other + '') : other; 00055 } 00056 if (typeof value != 'string') { 00057 return value === 0 ? value : +value; 00058 } 00059 value = value.replace(reTrim, ''); 00060 var isBinary = reIsBinary.test(value); 00061 return (isBinary || reIsOctal.test(value)) 00062 ? freeParseInt(value.slice(2), isBinary ? 2 : 8) 00063 : (reIsBadHex.test(value) ? NAN : +value); 00064 } 00065 00066 module.exports = toNumber;