toNumber.js
575 Bytes
var isArray = require('./isArray');
/**
* covert value into number if numeric
*/
function toNumber(val){
// numberic values should come first because of -0
if (typeof val === 'number') return val;
// we want all falsy values (besides -0) to return zero to avoid
// headaches
if (!val) return 0;
if (typeof val === 'string') return parseFloat(val);
// arrays are edge cases. `Number([4]) === 4`
if (isArray(val)) return NaN;
return Number(val);
}
module.exports = toNumber;