Yesterday I learned that these NPM packages exist:
- https://www.npmjs.com/package/is-odd
- https://www.npmjs.com/package/is-even
- https://www.npmjs.com/package/is-number
I have to admit that this both surprised and amused me.
I was under the opinion that the average NPM package is a little bit more contrived than this:
// is-odd
return (n % 2) === 1;
or this:
// is-number
if (typeof num === 'number') {
return num - num === 0;
}
But the thing that surprised me the most, is that developers actually use these packages as dependencies on their projects.
is-odd has 500k weekly downloads!
And what makes this whole thing even funnier is that this package has a dependency of it's own, and it is the above stated is-number package.
So the final size of is-odd comes down to cca. 6.5kB.
I don't understand why this package, nor it's sister is-even package, are so popular when it is so easy to "implement" the functionality they offer with vanilla JS (it requires just a single line of code).
But don't get me wrong. I am not trying to be negative.
Who knows why these packages exists and why they became so popular.
For example, one of the reasons might be that the author initially created is-odd because he was practicing how to publish a package to NPM.
But this is just conjecture at this point and it is irrelevant to the remainder of this post and moral of the story :)
I just wanted to explain my motivation before I came to the main topic of the post.
I present to you my very own, and first, published NPM package called linear-array
Here it is: https://www.npmjs.com/package/linear-array
Just to be clear, I am completely aware of the actual uselessness of this package.
But I decided to create it anyway because of the already stated reasons, and more importantly, because I wanted to learn how NPM packages are published.
What it does:
Returns an array filed with linearly increasing numbers, starting from 0 up to the given value - 1 (without offset), or from 1 to the value itself (with offset).
How to use it:
import linearArray from 'linear-array'; // web
OR
const linearArray = require('linear-array'); // server
console.log(linearArray(1)); //=> [0]
console.log(linearArray(3)); //=> [0,1,2]
console.log(linearArray(5)); //=> [0,1,2,3,4]
console.log(linearArray(1, true)); //=> [1]
console.log(linearArray(3, true)); //=> [1,2,3]
console.log(linearArray(5, true)); //=> [1,2,3,4,5]
So it turns out that all of this actually isn't very complicated.
Here's a short workflow on how to do it:
- Think of a package that you could create and a unique name (check on the NPM repository that the name is not already taken)
- Create a local folder with the same name as your future NPM package
- Add the necessary files (index.js, README, LICENSE, and test.js if you want) and fill it with markdown and code
- Run
git init
in your terminal - Push to Github repo with the same name as the local project name
- Run
npm init
in your terminal - Register your profile on https://www.npmjs.com/
- Run
npm login
in your terminal - Run
npm publish
in your terminal
Take a look of the project repo of linear-array if you get stuck somewhere.
That's it.
Thanks for reading this post until the end.
The moral of the story is, it doesn't matter if you think that your idea for an NPM package is a shitty one.
What does matter is the learning journey.
And because it is fun to try new things like this.
P.S.
If you actually find some use for my package and decide to install it on your project, I'm begging you to reconsider and that you don't do actually do it. 😂
Just copy the code directly from the index.js file on the project repo page.
Cheers!
Top comments (17)
Haha I had a very similar post planned - you beat me to it!
The
is-even
package has a dependency -is-odd
lololYeah, but that is some genius level stuff :)
Lol the only thing left is to get it into the react core or something
I was surprised with the is-odd and is-even download amount. But you know, sometimes you just need to know wether it is or not.
Those useless packages have so many downloads, because some basic,useful package use it. And for example webpack use this useful package. So now everyone installing webpack or something that uses webpack (basically any top is framework) is counted as download.
would be more appropriate to say that webpack doesn't actually use is-odd and is-even, one of the dependencies uses it.
I think the idea of your package is actually pretty useful. Just found something similar that has ~2k weekly downloads.
npmjs.com/package/range
My guess is it's just preference for is-odd and is-even.
JS community fascinates me every passing day
Well, that's odd!
Keeps the playing field even!
Gotta check the numbers on that one
thank you for your short work ...
No problem. Thank you for taking the time to read my post.
Why is line "num - num === 0" needed?
Ambar explained it pretty well.
I think the best, and easiest way, to check if a variable is of type 'number' in JS, is to use the
method.
It will weed out anything that is not a number, including NaN and Infinity.
If simply true is returned, input NaN (not a number) will also return true. It turns out typeof NaN is number. Also NaN - NaN is NaN so this expression num - num === 0 returns true only when num is a number except NaN.
Yeah. And same goes for Infinity and -Infinity.