| 1 |
Hashish |
| 2 |
======= |
| 3 |
|
| 4 |
Hashish is a node.js library for manipulating hash data structures. |
| 5 |
It is distilled from the finest that ruby, perl, and haskell have to offer by |
| 6 |
way of hash/map interfaces. |
| 7 |
|
| 8 |
Hashish provides a chaining interface, where you can do: |
| 9 |
|
| 10 |
var Hash = require('hashish'); |
| 11 |
|
| 12 |
Hash({ a : 1, b : 2, c : 3, d : 4 }) |
| 13 |
.map(function (x) { return x * 10 }) |
| 14 |
.filter(function (x) { return x < 30 }) |
| 15 |
.forEach(function (x, key) { |
| 16 |
console.log(key + ' => ' + x); |
| 17 |
}) |
| 18 |
; |
| 19 |
|
| 20 |
Output: |
| 21 |
|
| 22 |
a => 10 |
| 23 |
b => 20 |
| 24 |
|
| 25 |
Some functions and attributes in the chaining interface are terminal, like |
| 26 |
`.items` or `.detect()`. They return values of their own instead of the chain |
| 27 |
context. |
| 28 |
|
| 29 |
Each function in the chainable interface is also attached to `Hash` in chainless |
| 30 |
form: |
| 31 |
|
| 32 |
var Hash = require('hashish'); |
| 33 |
var obj = { a : 1, b : 2, c : 3, d : 4 }; |
| 34 |
|
| 35 |
var mapped = Hash.map(obj, function (x) { |
| 36 |
return x * 10 |
| 37 |
}); |
| 38 |
|
| 39 |
console.dir(mapped); |
| 40 |
|
| 41 |
Output: |
| 42 |
|
| 43 |
{ a: 10, b: 20, c: 30, d: 40 } |
| 44 |
|
| 45 |
In either case, the 'this' context of the function calls is the same object that |
| 46 |
the chained functions return, so you can make nested chains. |
| 47 |
|
| 48 |
Methods |
| 49 |
======= |
| 50 |
|
| 51 |
forEach(cb) |
| 52 |
----------- |
| 53 |
|
| 54 |
For each key/value in the hash, calls `cb(value, key)`. |
| 55 |
|
| 56 |
map(cb) |
| 57 |
------- |
| 58 |
|
| 59 |
For each key/value in the hash, calls `cb(value, key)`. |
| 60 |
The return value of `cb` is the new value at `key` in the resulting hash. |
| 61 |
|
| 62 |
filter(cb) |
| 63 |
---------- |
| 64 |
|
| 65 |
For each key/value in the hash, calls `cb(value, key)`. |
| 66 |
The resulting hash omits key/value pairs where `cb` returned a falsy value. |
| 67 |
|
| 68 |
detect(cb) |
| 69 |
---------- |
| 70 |
|
| 71 |
Returns the first value in the hash for which `cb(value, key)` is non-falsy. |
| 72 |
Order of hashes is not well-defined so watch out for that. |
| 73 |
|
| 74 |
reduce(cb) |
| 75 |
---------- |
| 76 |
|
| 77 |
Returns the accumulated value of a left-fold over the key/value pairs. |
| 78 |
|
| 79 |
some(cb) |
| 80 |
-------- |
| 81 |
|
| 82 |
Returns a boolean: whether or not `cb(value, key)` ever returned a non-falsy |
| 83 |
value. |
| 84 |
|
| 85 |
update(obj1, [obj2, obj3, ...]) |
| 86 |
----------- |
| 87 |
|
| 88 |
Mutate the context hash, merging the key/value pairs from the passed objects |
| 89 |
and overwriting keys from the context hash if the current `obj` has keys of |
| 90 |
the same name. Falsy arguments are silently ignored. |
| 91 |
|
| 92 |
updateAll([ obj1, obj2, ... ]) |
| 93 |
------------------------------ |
| 94 |
|
| 95 |
Like multi-argument `update()` but operate on an array directly. |
| 96 |
|
| 97 |
merge(obj1, [obj2, obj3, ...]) |
| 98 |
---------- |
| 99 |
|
| 100 |
Merge the key/value pairs from the passed objects into the resultant hash |
| 101 |
without modifying the context hash. Falsy arguments are silently ignored. |
| 102 |
|
| 103 |
mergeAll([ obj1, obj2, ... ]) |
| 104 |
------------------------------ |
| 105 |
|
| 106 |
Like multi-argument `merge()` but operate on an array directly. |
| 107 |
|
| 108 |
has(key) |
| 109 |
-------- |
| 110 |
|
| 111 |
Return whether the hash has a key, `key`. |
| 112 |
|
| 113 |
valuesAt(keys) |
| 114 |
-------------- |
| 115 |
|
| 116 |
Return an Array with the values at the keys from `keys`. |
| 117 |
|
| 118 |
tap(cb) |
| 119 |
------- |
| 120 |
|
| 121 |
Call `cb` with the present raw hash. |
| 122 |
This function is chainable. |
| 123 |
|
| 124 |
extract(keys) |
| 125 |
------------- |
| 126 |
|
| 127 |
Filter by including only those keys in `keys` in the resulting hash. |
| 128 |
|
| 129 |
exclude(keys) |
| 130 |
------------- |
| 131 |
|
| 132 |
Filter by excluding those keys in `keys` in the resulting hash. |
| 133 |
|
| 134 |
Attributes |
| 135 |
========== |
| 136 |
|
| 137 |
These are attributes in the chaining interface and functions in the `Hash.xxx` |
| 138 |
interface. |
| 139 |
|
| 140 |
keys |
| 141 |
---- |
| 142 |
|
| 143 |
Return all the enumerable attribute keys in the hash. |
| 144 |
|
| 145 |
values |
| 146 |
------ |
| 147 |
|
| 148 |
Return all the enumerable attribute values in the hash. |
| 149 |
|
| 150 |
compact |
| 151 |
------- |
| 152 |
|
| 153 |
Filter out values which are `=== undefined`. |
| 154 |
|
| 155 |
clone |
| 156 |
----- |
| 157 |
|
| 158 |
Make a deep copy of the hash. |
| 159 |
|
| 160 |
copy |
| 161 |
---- |
| 162 |
|
| 163 |
Make a shallow copy of the hash. |
| 164 |
|
| 165 |
length |
| 166 |
------ |
| 167 |
|
| 168 |
Return the number of key/value pairs in the hash. |
| 169 |
Note: use `Hash.size()` for non-chain mode. |
| 170 |
|
| 171 |
size |
| 172 |
---- |
| 173 |
|
| 174 |
Alias for `length` since `Hash.length` is masked by `Function.prototype`. |
| 175 |
|
| 176 |
See Also |
| 177 |
======== |
| 178 |
|
| 179 |
See also [creationix's pattern/hash](http://github.com/creationix/pattern), |
| 180 |
which does a similar thing except with hash inputs and array outputs. |
| 181 |
|
| 182 |
Installation |
| 183 |
============ |
| 184 |
|
| 185 |
To install with [npm](http://github.com/isaacs/npm): |
| 186 |
|
| 187 |
npm install hashish |
| 188 |
|
| 189 |
To run the tests with [expresso](http://github.com/visionmedia/expresso): |
| 190 |
|
| 191 |
expresso |