'use strict';
var isOdd = require('is-odd');
module.exports = function isEven(i) {
return !isOdd(i);
}; 'use strict';
var isOdd = require('is-odd');
module.exports = function isEven(i) {
return !isOdd(i);
}; 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;
}; '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;
};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.
Which is weird since they use the isFinite built-in right after that, but I have no idea why they throw in all the string checks and trims and + for type coercion, and then use the global isFinite function which does all that internally. In fact the global function isFinite does everything this package does
(something I just learned tho checking this code out is that there is a global isNaN and isFinite which coerce strings into numbers automatically, and Number.isNaN and Number.isFinite which function similarly but without coercing the string first, so I guess this package exists as a learning aide of all the different ways to ask if something is a number)
EDIT: I now know why they check if a string input is not an empty string, because empty strings get coerced to 0, so isFinite("") returns true.
Yes, when coerced with +:
Number.isFinite(+'') // true
Number.isFinite('') // falseHence the heading "Confusing Special Case Behavior" in the mdn docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
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.
I agree most people these days should just use Number.isNaN, assuming they don't need to support IE.
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
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.
[1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Or are are all those extra checks unnecessary now that TypeScript exists and new functions like Number.isInteger have widespread browser support?
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.
var isOdd = require("is-odd");
console.log(isOdd([1])); // TRUE!! 'use strict';
var isEven = require('is-even');
module.exports = function isOdd(i) {
return !isEven(i);
}; function isOdd(i) {
return i > 0 && isEven(i - 1);
}
function isEven(i) {
return i == 0 || isOdd(i - 1);
}https://www.npmjs.com/package/is-even-cyclic has a cyclic dependency with https://www.npmjs.com/package/is-odd-cyclic.