4 ms·
Ruby: x.is_a? Integer PHP: is_int($x) (alias of is_integer) Python: isinstance(x, int) Node.js[1]: typeof val === "number" && (!(typeof value !== 'n
by captn3m0 6y ago
Ruby: x.is_a? Integer
PHP: is_int($x) (alias of is_integer)
Python: isinstance(x, int)
Node.js[1]:
typeof val === "number" &&
(!(typeof value !== 'number' || value !== value || value === Infinity || value === -Infinity)) &&
Math.floor(val) === val
I think all supported Node.js versions now have Number.isInteger (via es262), but the is-integer package still gets a 200_000 downloads a week[2].
[1]: https://github.com/parshap/js-is-integer/blob/master/index.js#L4-L8 https://github.com/parshap/js-is-integer/blob/master/index.j...
[2]: https://www.npmjs.com/package/is-integer https://www.npmjs.com/package/is-integer
- Xavdidtheshadow 6y agoThis actually taps into something I really like about npm. For things that "should be easy/obvious", there are usually edge cases, _especially_ around how `Object` is handled. The real answer is for Node to have built-ins that handle this (which they now do for `isInteger`, but absent that, I love that there are these specific packages that have unit tests and a simple API. People scoff at things at `isPromise`[1], but it's reliably filling a super important niche that the language doesn't handle. [1]: https://github.com/then/is-promise/blob/1d547300e780a4eca391236f15e5ccbf76a5789d/index.js https://github.com/then/is-promise/blob/1d547300e780a4eca391...
- captn3m0 6y agoMaybe we should adopt these packages into es262 and make the shims available by default directly with the language?
- deleted 6y ago[deleted]
- phiresky 6y agoYou could have found a better example. The package you referenced literally just does `Number.isInteger` unless you are on decade old versions of nodejs
- captn3m0 6y agoIt still gets downloaded and the code still runs. 200_000 times a week, irrespective of your node version. Even when the language migrates to a standard (NumberisInteger), the remaining ecosystem keeps depending on this package.