New Way to Manage JavaScript Utilities

3 min readJul 20, 2016

I’m sure most of you have heard about the ‘left-pad & npm’ stuff that raised a lot of discussion a few months ago. In my view, the main focus of the debate is all about whether you should publish single JavaScript function as an individual npm package. My answer is no. It just doesn’t feel right when installing a package, it brings me another twenty dependencies that are mostly small functions. So what should we do with small JavaScript utilities? Here is how I deal with it.

I started by writing all utilities myself which was not as hard as I imagined. In fact, I was able to finish most of them within 30 minutes. For special cases like isArray or isNumber, less than one minute. I used to take a longer time searching npm for such modules. You know, when you typed ‘left pad’, there are packages called ‘left-pad, lpad, pad-left, pad_left, string-pad, xx-left-pad, left-pad-xx…’. You have to decide which is the right one to install. And usually ended up with the one that has most downloads. However, from what I have learned, in most cases for these tiny modules, they are downloaded with the highest number simply because they are registered with a better name, instead of having the best implementation. Another benefit of writing utilities myself is being able to fix a bug immediately. If you stick with using npm modules for that, you have to first open an issue in its repo, wait for the author to fix and then publish a new version. At last, you update you npm dependencies. It’s a decent process for complicate open source projects. However, clearly not an efficient one for modules written by only a few lines of code.

Now comes to the important part: How do I reuse all these utilities? I have written leftPad before in one of my projects. Certainly I am not going to try it a second time in another new project because it sounds silly. To solve this problem, I wrote a tool to help me generate utility libraries for each projects. Here is how it works. First I create a repo for storing utilities. The tool scan my new project files to see what I have included and then use code pieces from that utility repo to generate a util.js file. Everything is done automatically, no more copying and pasting!

The tool is open source on GitHub and I now used it in all my projects such as Eruda or FreeGo. If you are interested, give it a try and let me know if something needs to be improved.

By the way, the default utility repo is also ready for people to add much more useful code snippets. Looking forward to your pull requests:)

To summarize, it’s like a combination of a giant version of loadash and a mini version of webpack designed for bundling small code pieces only.

So there is a default repo containing lots of useful functions. If you need a utility function, search it inside the repo first. If not exists, pull request to add it or write it in your own special utility repo. At last use the tool to build what you need for your projects using codes from these two repositories, thus no npm involved.