Hacker News new | ask | show | jobs
by Ayesh 1814 days ago
Which itself depends on is-number package.
1 comments

You and GP have shown me new even more scary depths here. Not sure whether I should thank you for that. (When does the next flight off this rock go?)
It's not really that crazy. This package is a small amount of code, but it's important code (the same goes for this package's dependency is-number). This package shows up in the dependency trees of some popular packages, which is probably where most of the weekly downloads come from.

If you're writing straightforward application code where you already know you have a valid number, then this package probably isn't for you. You can just do num % 2 === 1.

I get it, JS is a weird language, but simple things like "is number" are still easy enough to do in JS, especially, when ints and floats are all just "number" in JS:

function is_number(val) { return typeof val === "number"; }

With a function that easily written, no one should have any excuse to depend on a third-party dependency for it.

But the code you posted is not what the code in this package does. If all you need is the code that you posted, then you should absolutely use that.
> not what the code in this package does

Here's `is-number` (https://github.com/jonschlinkert/is-number/blob/master/index...):

    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;
    };

Care to explain why these 5 lines of code need to be a package? To me, having something so simple be a package by itself is absurd.
Note that there’s a difference between “needs to be a package” and “needs to be used by everyone.” Nothing needs to be a package, because it’s always possible to copy paste code, regardless of how many lines it is.
leftpad was a small amount of code too. If it’s a small amount of code that’s design stable and downloaded often, it’s an extremely strong candidate for inclusion in the standard library.
It was added to the standard library: padStart