3 ms·
It's not just checking if the number is odd. It checks if the input is a number (modulus on a non-Number value returns a NaN in JS), if it's an integer (not str
by onion2k 6y ago
It's not just checking if the number is odd. It checks if the input is a number (modulus on a non-Number value returns a NaN in JS), if it's an integer (not strictly necessary I guess), and if it's within the bounds of JS's safe integer range (again, you'll get a NaN if the number is too big). If you want to make sure your code is safe those are sensible things to check.
A lot of JS devs wouldn't bother check those things, so really a lot of code is improved by this library. It's kind of a shame that so many developers just skip over checking inputs really. That's the real problem.
- berkes 6y agoThe obvious solution would be to have some stdlib or even a common-utils project or something like it, which contain all such items. In one package. Because, indeed, no-one should be rewriting obvious code like isArray, leftPad, isEven and so on. Yes, that might cause some overhead in some situations (when you need isOdd, but not leftPad). But I'm certain that overhead is minimal. In practice, however, we currently see dependency-trees where these "should-be-stdlib" modules like "leftPad" or "isArray" are repeated numerous times in one project. Any stdlib could even be optimized by browsers and engines, turning that "overhead" into a net benefit.
- eeereerews 6y agoIt doesn't. isOdd('') => false isOdd(null) => false isOdd(2n) => TypeError: Cannot convert a BigInt value to a number But even if it did, the same line of reasoning leads to the absurd conclusion we should have an addOne function.