Hacker News new | past | comments | ask | show | jobs | submit | unstable's comments login

> You can write isNumber(foo) instead of typeof foo === "number".

Indeed you can, but it depends what isNumber does. This is more like what it should do IMO:

function isNumber( foo ) { return ( (typeof foo === "number") && (foo == foo)) || ((typeof foo === 'object') && (foo instanceof Number) ); }

And that is I think the value of micro libs, at least in JS, you don't want to think about all the edge cases when you only want to check if something is a Number.


This library is a hilarious example of a huge problem with this kind of package. "Number" is in the eye of the beholder. A string containing numeric characters is, in my view, in no useful way "a number". A package that treats it as such just perpetuates weakly-typed nonsense.*

But the broader point is, you can't outsource understanding to a package. There will be places in your code where NaN is a perfectly valid number, or Infinity. And other places where you absolutely need to be sure neither of the above make their way in.

By pretending that a package can capture the universal essence of "numberless", and that this will broadly apply across the entire JS ecosystem (see reported benefits like "different libraries can all rely on is-number instead of rewriting duplicated helper functions!") is naive.

I wrote more about this in a post linked in a top level comment. The is-promise library is another great example.

* Personal pet theory is that the package author would have been embarrassed to publish a 1-line package, so included "numeric strings are numbers" as a fig leaf to justify the package's existence. They should have instead created two new packages, is-actual-number and is-numeric-string, so the implementation of is-number could be nice and clean:

    module.exports = function(n) { return require('is-actual-number')(n) || require('is-numeric-string')(n); }
I can feel the power of webscale coursing through me


This is an argument for having a library that provides that function, but it is not an argument that it should be a micro-library.


Doesn't that exclude NaN (which you probably want despite the name)? I think this really highlights that you probably do want to think about those edge cases...

In any case this is a bad example because Typescript exists.


Accepting NaN as a number can potentially crash your app, that's why I reject it in isNumber.

I only tried to highlight some edge cases that I personally don't like to spend energy on, trying to get it right, when writing code. Btw, isNumber is a dynamic call in the example and unrelated to TypeScript. TypeScript doesn't exist at runtime.


Reminds me of the 2ality blog post on that:

https://2ality.com/2017/08/type-right.html


> while the whole thing should be on the editor's side.

This.

I'd prefer using my own preferences instead of using a dumb formatter like prettier. Who cares some other dev in your team prefers 2 chars indentation? It should be an editor preference, not applied to source code.

Personally I would also prefer the IDE to do type checking as well so we can get rid of the TypeScript misery, but that's another discussion.


Thanks for the links, awesome!


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: