fast-isnumeric
Version:
The fast way to check if a JS object is numeric
25 lines (20 loc) • 627 B
JavaScript
/**
* inspired by is-number <https://github.com/jonschlinkert/is-number>
* but significantly simplified and sped up by ignoring number and string constructors
* ie these return false:
* new Number(1)
* new String('1')
*/
;
var allBlankCharCodes = require('is-string-blank');
module.exports = function(n) {
var type = typeof n;
if(type === 'string') {
var original = n;
n = +n;
// whitespace strings cast to zero - filter them out
if(n===0 && allBlankCharCodes(original)) return false;
}
else if(type !== 'number') return false;
return n - n < 1;
};