3 ms·
There's also a 'is-even' package, which depends on 'is-odd', which in turn, depends on 'is-number'. 'is-odd' is a little bit more involved than 'is-even'. What
by karim79 3y ago
There's also a 'is-even' package, which depends on 'is-odd', which in turn, depends on 'is-number'.
'is-odd' is a little bit more involved than 'is-even'. What it does, is:
> Returns true if the given number is odd, and is an integer that does not exceed the JavaScript MAXIMUM_SAFE_INTEGER.[0]
The full implementation of 'is-number' is as follows:
'use strict';
module.exports = function(num) {
if (typeof num === 'number') {
return num - num === 0;
}
if (typeof num === 'string' && num.trim() !== '') {
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
}
return false;
};
Ridiculous as it may be, my guess is that provides a unified way of number checking while avoiding the pitfalls or crazy consequences which could arise from divide-by-zero[1], which I guess might prevent less-experienced folks from tripping up on some of the Javascript gotchas? I'm not really sure. That being said, I have seen those package dependencies in some serious stuff.
Do I agree? I'm not really sure to be honest. Probably not.
[0] https://github.com/i-voted-for-trump/is-odd/blob/master/README.md https://github.com/i-voted-for-trump/is-odd/blob/master/READ...
[1] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinitehttps://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isFinite https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
EDIT: removed redundant sentence
- weare138 3y agoI think that's really the issue here. It's not that these packages exist and aren't useful to less experienced devs or people new to JS just trying to get a task done, it's these packages keep showing up in professional/enterprise apps and libraries. Those devs should already know how to do this stuff. It seems odd these projects would leave themselves vulnerable to supply chain attacks over something so trivial.
- ElevenLathe 3y agoReally? I'm not a JavaScript dev but this is pretty inscrutable. I think its totally reasonable for devs to expect not to have to do all this byzantine casting and comparisons just to find out if a variable contains a number. You're right in the sense that an experienced dev should know to vendor this or reimplement in code they control rather than downloading it every time they build, but the necessity of this library is more of a damnation of the JS stdlib than anything.