This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->
node logo

Hello everyone! This is the fifth post in my new node.js modules you should know about article series.

The first post was about dnode - the freestyle rpc library for node, the second was about optimist - the lightweight options parser for node, the third was about lazy - lazy lists for node, the fourth was about request - the swiss army knife of HTTP streaming.

This time I'll introduce you to hashish. Hashish is written by James Halliday, who's my co-founder of Browserling and Testling. In case you're wondering why I am blogging about so many of his modules, it's because he's written 88 of them and each one of them is absolutely brilliant.

Hashish is a JavaScript hash combinator library, or in other words, it contains a bunch of hash data structure manipulation functions.

Check out this example,

var Hash = require('hashish');

Hash({ a : 1, b : 2, c : 3, d : 4 })
    .map(function (x) { return x * 10 })
    .filter(function (x) { return x < 30 })
    .forEach(function (x, key) {
        console.log(key + ' => ' + x);
    })
;

Here a Hash object is constructed from the hash { a : 1, b : 2, c : 3, d : 4 }. Next, a function that multiplies each hash value by 10 is mapped over. At this moment the hash has become { a : 10, b : 20, c : 30, d : 40 } Then a filter is applied that filters only hash elements that have value less than 30. At this point hash is { a : 10, b : 20 }. Finally forEach combinator is applied all the elements that are left and the key, value pair is printed, producing the following output:

a => 10
b => 20

Notice how similar the interface for hash manipulation is to the node-lazy that I wrote about a few days ago. All the combinators can be chained so your code stays beautiful.

If you can't or don't want to chain the functions, hashish also allows each function in the chainable interface to be attached to Hash in chainless form:

var Hash = require('hashish');
var obj = { a : 1, b : 2, c : 3, d : 4 };

var mapped = Hash.map(obj, function (x) {
    return x * 10
});

console.dir(mapped);

Notice how this code calls Hash.map on obj hash. The output is each hash value multiplied by 10:

{ a: 10, b: 20, c: 30, d: 40 }

Hashish also provides various attributes in the chaining interface and functions in the Hash.xxx interface. For example:

$ node
> var Hash = require('hashish');
> var obj = { a : 1, b : 2, c : 3, d : 4 };
>
> Hash(obj).keys
[ 'a', 'b', 'c', 'd' ]
> Hash(obj).values
[ 1, 2, 3, 4 ]
> Hash(obj).length
4

You can install hashish through npm:

npm install hashish

Sponsor this blog series!

Doing a node.js company and want your ad to appear in the series? The ad will go out to 14,000 rss subscribers, 7,000 email subscribers, and it will get viewed by thousands of my blog visitors! Email me and we'll set it up!

Enjoy!

If you love these articles, subscribe to my blog for more, follow me on Twitter to find about my adventures, and watch me produce code on GitHub!

This article is part of the article series "Node.JS Modules You Should Know About."
<- previous article next article ->

Comments

December 07, 2011, 19:42

This looks nice and useful, but I currently do that same kind of thing with plain JS objects in Node using underscore.js and its chain() function.

December 07, 2011, 20:22

I like this library as it's really small and well defined.

December 08, 2011, 05:00

That's sweet. I see you can also filter on the keys of the hash using the second argument. So you could get all items with keys matching a regex, which could be useful for searching.

December 02, 2012, 05:43

I like it because it's... like... hashish, maaan. far out. That's a funny name though, I enjoy the wordplay. Seems like a useful module for any effective data mining or processing operation. I might be able to couple it with a natural language processing platform I'm wanting to start working on, because node.js seems to be just the way i want to go for development. Thanks for sharing!

December 05, 2014, 20:16

Liked these very much,You have made my day bro.
new year wishes messages to you.

December 05, 2014, 20:16

Liked these very much,You have made my day bro.
new year wishes messages to you.

Leave a new comment

(why do I need your e-mail?)

(Your twitter name, if you have one. (I'm @pkrumins, btw.))

Type the word "security_287": (just to make sure you're a human)

Please preview the comment before submitting to make sure it's OK.

Advertisements