isNumber would make for a legit package. Is there a good way in javascript to determine confidently if an arbitrary input is a number?
e.g: [1, "one", true, false, 0, "zero", null, undefined]
edit: also ["1", "0", etc]
edit 2: also [NaN]
e.g: [1, "one", true, false, 0, "zero", null, undefined]
edit: also ["1", "0", etc]
edit 2: also [NaN]
typeof v === "number"
The rest is parsing territory. “number" == typeof variable
Works for decimals, too.Although maybe you meant to include
"1"
In your examples? "number" === typeof NaN
Returns true.Now, the fact that IEEE754 is insane is something else, but JS does nothing wrong (aside from not having not-floats)
JS has BigInt - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
const allowNumbers = {
all: () => true,
notNaN: n => !Number.isNaN(n),
finite: Number.isFinite,
integer: Number.isInteger,
smol: n => Math.abs(n) < Number.EPSILON,
lorge: n => n > Number.MAX_SAFE_INTEGER || n < Number.MIN_SAFE_INTEGER,
}
function isNumber(n, allow = allowNumbers.all) {
return ("number" === typeof n) && allow(n);
}Any weird edge cases against this one?