3 ms·
Anyone who needs to test an input is an odd number using vanilla JS will eventually end up rolling something that looks a lot like isOdd() themselves (or they'l
by onion2k 6y ago
Anyone who needs to test an input is an odd number using vanilla JS will eventually end up rolling something that looks a lot like isOdd() themselves (or they'll just ignore a set of potential problems, which definitely is worse). It's very obvious when you read the code - https://github.com/jonschlinkert/is-odd/blob/master/index.js https://github.com/jonschlinkert/is-odd/blob/master/index.js.
Utilities like that are a function of using a weakly typed language. "Just use TypeScript" is an alternative solution, but even in TS you should still check the number is within the bounds of isSafeInteger.
- rozab 6y agoBut why should isOdd, isEven and isNumber reside in seperate packages? Surely a single math tools package should cover all of this?
- wongarsu 6y agoIt's an overreaction to the critizism of bloated libraries like JQuery. Dead code removal with tree shaking is a relatively recent feature in JS toolchains, so instead you have single-function libraries. For example there are also npm packages with each lodash function as its own package [1] 1: https://www.npmjs.com/search?q=keywords:lodash-modularized https://www.npmjs.com/search?q=keywords:lodash-modularized
- z3t4 6y agoThink of it like being able to pull any function from a global scope. eg. var/const foo = require("bar") Its one gigantic standard library.
- onion2k 6y agoSurely a single math tools package should cover all of this? They shouldn't. It's silly. You're right that there should be "math"[1], but while those things aren't part of the main JS ecosystem people write utility libraries and publish them. [1] There is, it's https://mathjs.org/ https://mathjs.org/. More people should use it.
- Carpetsmoker 6y agoAre those checks actually needed? "2.2 % 2 === 1" works just as well, so why is the isInteger check needed? Why is the isSafeInteger check needed? And "a % 2" is already NaN (and thus false), although an explicit error is arguably better than silently eating it.