5 ms·
The entire package boils down to the following. I find it funny. 'use strict'; var isOdd = require('is-odd'); module.exports = function isEven(i)
by sethammons 5y ago
The entire package boils down to the following. I find it funny.
'use strict';
var isOdd = require('is-odd');
module.exports = function isEven(i) {
return !isOdd(i);
};
- maxbaines 5y agoYes and is-odd contains const isNumber = require('is-number'); module.exports = function isOdd(value) { const n = Math.abs(value); if (!isNumber(n)) { throw new TypeError('expected a number'); } if (!Number.isInteger(n)) { throw new Error('expected an integer'); } if (!Number.isSafeInteger(n)) { throw new Error('value exceeds maximum safe integer'); } return (n % 2) === 1; };
- pibi 5y agoI love the fact that this small code is still wrong: var isOdd = require("is-odd"); console.log(isOdd([1])); // TRUE!!
- maxbaines 5y agoFor good measure is-number contains '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; };
- deleted 5y ago[deleted]
- rahkiin 5y agoFor those wondering: num-num===0 is a check for NaN.
- afavour 5y agoSmart, but isNaN already exists… https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- Ajedi32 5y agoIt didn't at the time this library was created. I assume now that line only exists for backwards compatibility.
- jazzyjackson 5y agothis library uses the global isFinite, which actually you could just use instead of the whole function, since it does the string coercion and infinity check for you. Check out the compatibility table on MDN, Chrome Firefox and Safari version 1 all support it, so I'm pretty sure isNaN and isFinite were in the original spec.
- Ajedi32 5y agoSee the MDN section on "confusing special case behavior"[1] for why they probably didn't want to use the global version of isNaN. Number.isNaN handles those cases better but is not as widely supported. [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#confusing_special-case_behavior https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- jazzyjackson 5y agoThanks for this, I had forgot that empty strings get coerced to 0... interesting choice.
- afavour 5y agoThe MDN compatibility data says IsNaN was in node as of 0.10.0, which was released 2013-03-11. NPM says the library was first published 2014-09-21. And the library uses isFinite which was added in the same Node version. So... not really, no. Anyway, my point wasn't so much that the code is an outrage, more that should anyone stumble across this comment thread they know that there is a much clearer and more obvious way to check for IsNaN than using x-x===0.
- deleted 5y ago[deleted]
- Ajedi32 5y agoSo in conclusion... maybe checking whether a number is even in JS isn't as trivial as everyone thought, and projects reusing that code was a good thing? Or are are all those extra checks unnecessary now that TypeScript exists and new functions like Number.isInteger have widespread browser support?
- hmrr 5y agoI think the assertion (sorry for the pun) is that JavaScript is a completely unholy creation and should be chucked off the edge of the disc. I can’t imagine trying to get some of our fintech business logic in there without arming a nuclear foot gun or ten. I would never sleep again without medication.
- mannerheim 5y agoIf so, the implementation of isEven isn't even right because it assumes anything that isn't an odd number is an even number.
- Ajedi32 5y agoNo, the code posted in this thread clearly indicates it will raise an exception in such cases.
- mannerheim 5y agoAh, that's right, never mind.
- runes 5y agoTypeScript can't help at all, it vanishes before the code runs.
- tinco 5y agoThat horrible realisation that because of its dependency on is-number, and all of its checks and pretty error messages I can imagine there's actually some valid use cases for this package. You can plug this right into a user input validation system and it would work perfect for production use cases in a way that num % 2 === 0 just wouldn't. Almost other use case for it though.. horrible. This package would be much better if it was in some sort of namespace that indicated it's for user input validation only.
- eurekin 5y agoImagine, if isOdd suddenly changed to: 'use strict'; var isEven = require('is-even'); module.exports = function isOdd(i) { return !isEven(i); };
- 2fast4you 5y agoYou joke, but: https://www.npmjs.com/package/is-uneven https://www.npmjs.com/package/is-uneven
- f00zz 5y agofunction isOdd(i) { return i > 0 && isEven(i - 1); } function isEven(i) { return i == 0 || isOdd(i - 1); }
- the_gipsy 5y agoI did it. https://www.npmjs.com/package/is-even-cyclic https://www.npmjs.com/package/is-even-cyclic has a cyclic dependency with https://www.npmjs.com/package/is-odd-cyclic https://www.npmjs.com/package/is-odd-cyclic.
- mbesto 5y agoAnd has 100% test code coverage. Brilliant.