I've found a few situations where having a couple of extensions added to the built in objects would be immensly helpful. It also helps avoid a lot of arduous boiler plate coding that can accompany javascript in node.js

If you're new to Node.js and you're saying "great, thanks for showing me this great extension thing, but I don't want to put it in every one of my files, that's a lot of work". Scroll below to find out more


Sometimes you get an array and you just want the unique values from it. Well you can write your own loop using a map or we can just use the built in filter command with predefined wrapper and implementation and we'll attach it to the prototype and call it unique

Array.prototype.unique = function () {
    return this.filter(function() {
                var seen = {};
                return function(element, index, array) {
                    return !(element in seen) && (seen[element] = 1);

I'll update this thread as I find more that are super awesome.

Including Utils

There is a way to get it down to one require per js file that you want to use these utilities in. If you're not aware yet, but Node.js is heavily conventions based, and if you really didn't know that, then go do some reading on node. You would be doing yourself a disservice if you didn't understand their choices. Yes it's javascript, but scope is managed differently, and you don't have a DOM and all sorts of unique quirks. Moving along...

  1. Create a ./lib folder in your project. If you already have a location for local library code then use that path if you prefer
  2. If no files in ./lib create an index.js (more on this specific name in a second)
  3. Paste the above code in the file
  4. Now if you want this extended prototype to be availble in any of your js files you would need to do a require('./lib')



var myArray = ["Item 1", "Item 2", "Item 3", "Item 2"]

console.log("myArray count: " + myArray.length )
console.log("unique count: " + myArray.unique().length)

Sample Output:

> node index.js

myArray count: 4
unique count: 3