1 | # Object-Hash
|
2 |
|
3 | ![status](https://secure.travis-ci.org/puleos/object-hash.png?branch=master)
|
4 |
|
5 | ![testling](https://ci.testling.com/puleos/object-hash.png?v=0.2.0)
|
6 |
|
7 | Generate hashes from objects and values in node and the browser. Uses node.js
|
8 | crypo module for hashing. Supports sha1, md5 and many others (depending on the platform).
|
9 |
|
10 | * Hash values of any type.
|
11 | * Provides a hash table implementation.
|
12 | * Supports a keys only option for grouping like objects with different values.
|
13 |
|
14 | ```
|
15 | var hash = require('object-hash');
|
16 | ```
|
17 | ## hash(value, options);
|
18 | Generate a hash from any object or type. Defaults to sha1 with hex encoding.
|
19 | * `algorithm` hash algo to be used: 'sha1', 'md5'. default: sha1
|
20 | * `excludeValues` {true|false} hash object keys, values ignored. default: false
|
21 | * `encoding` hash encoding, supports 'buffer', 'hex', 'binary', 'base64'. default: hex
|
22 |
|
23 | ## hash.sha1(value);
|
24 | Hash using the sha1 algorithm.
|
25 |
|
26 | *Sugar method, equivalent to hash(value, {algorithm: 'sha1'})*
|
27 |
|
28 | ## hash.keys(value);
|
29 | Hash object keys using the sha1 algorithm, values ignored.
|
30 |
|
31 | *Sugar method, equivalent to hash(value, {excludeValues: true})*
|
32 |
|
33 | ## hash.MD5(value);
|
34 | Hash using the md5 algorithm.
|
35 |
|
36 | *Sugar method, equivalent to hash(value, {algorithm: 'md5'})*
|
37 |
|
38 | ## hash.keysMD5(value);
|
39 | Hash object keys using the md5 algorithm, values ignored.
|
40 |
|
41 | *Sugar method, equivalent to hash(value, {algorithm: 'md5', excludeValues: true})*
|
42 |
|
43 | ## var hashTable = new hash.HashTable(options);
|
44 | Create a new HashTable instance. Hashing options are supported and applied to all values
|
45 | added to the table.
|
46 |
|
47 | ## hashTable.add(value1, value2, ...);
|
48 | Add an object to the hash table. Supports n parameters or an array of values to be
|
49 | added to the table.
|
50 |
|
51 | *Note: if you wish to evaluate an array as a single table entry
|
52 | you must wrap it first `{[1,2,3,4]}` otherwise each element will be added to the
|
53 | table separately.*
|
54 |
|
55 | ## hashTable.getValue(hashKey);
|
56 | Retrive the objects value from the table by hash key. If there is no matching entry
|
57 | returns undefined.
|
58 |
|
59 | ## hashTable.getCount(hashKey);
|
60 | Retrieve a counter representing the number of times an object was added to
|
61 | the table. Returns 0 if a matching key is not found.
|
62 |
|
63 | ## hashTable.hasKey(hashKey);
|
64 | Returns true if the specified hash is in the hash table otherwise false.
|
65 |
|
66 | ## hashTable.toArray();
|
67 | Returns an array of the hash table contents in the following format:
|
68 | ```
|
69 | [ {hash:'14fa461bf4b98155e82adc86532938553b4d33a9',
|
70 | count: 2, value: {foo: 'bar', baz: true }},
|
71 | {hash:'14fa461bf4b98155e82adc86532938553b4d33a9',
|
72 | count: 1, value: {foo: 'bar', baz: true }} ]
|
73 | ```
|
74 | *Note: when the excludeValues option is set, the `value` of the hash table is an array of objects with matching keys.*
|
75 |
|
76 | ## hashTable.reset();
|
77 | Clears the contents of the hash table.
|
78 |
|
79 | ## Installation
|
80 |
|
81 | node:
|
82 | ```
|
83 | npm install object-hash
|
84 | ```
|
85 |
|
86 | browser: */dist/object_hash.js*
|
87 | ```
|
88 | <script src="object_hash.js" type="text/javascript"></script>
|
89 | ```
|
90 |
|
91 | ## Example usage
|
92 | ```js
|
93 | var hash = require('object-hash');
|
94 |
|
95 | var peter = {name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
|
96 | var michael = {name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] };
|
97 | var bob = {name: 'Bob', stapler: true, friends: [] };
|
98 |
|
99 | /***
|
100 | * sha1 hex encoding (default)
|
101 | */
|
102 | hash(peter);
|
103 | // 14fa461bf4b98155e82adc86532938553b4d33a9
|
104 | hash(michael);
|
105 | // 4b2b30e27699979ce46714253bc2213010db039c
|
106 | hash(bob);
|
107 | // 38d96106bc8ef3d8bd369b99bb6972702c9826d5
|
108 |
|
109 | /***
|
110 | * hash object keys, values ignored
|
111 | */
|
112 | hash(peter, { excludeValues: true });
|
113 | // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
|
114 | hash(michael, { excludeValues: true });
|
115 | // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
|
116 | hash.keys(bob);
|
117 | // 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
|
118 |
|
119 | /***
|
120 | * md5 base64 encoding
|
121 | */
|
122 | hash(peter, { algorithm: 'md5', encoding: 'base64' });
|
123 | // 6rkWaaDiG3NynWw4svGH7g==
|
124 | hash(michael, { algorithm: 'md5', encoding: 'base64' });
|
125 | // djXaWpuWVJeOF8Sb6SFFNg==
|
126 | hash(bob, { algorithm: 'md5', encoding: 'base64' });
|
127 | // lFzkw/IJ8/12jZI0rQeS3w==
|
128 |
|
129 | /***
|
130 | * HashTable example
|
131 | */
|
132 | var hashTable = new hash.HashTable();
|
133 | var peterHash = hash(peter);
|
134 |
|
135 | hashTable.add(peter, michael, bob);
|
136 | hashTable.getValue(peterHash);
|
137 | // {name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
|
138 | hashTable.getCount(peterHash);
|
139 | // 1
|
140 | hashTable.add({name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] });
|
141 | hashTable.getCount(peterHash);
|
142 | // 2
|
143 | hashTable.hasKey(peterHash);
|
144 | // true
|
145 | hashTable.toArray();
|
146 | // [ {hash:'14fa461bf4b98155e82adc86532938553b4d33a9',
|
147 | // count: 2, value: {name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] }},
|
148 | // {hash:'4b2b30e27699979ce46714253bc2213010db039c',
|
149 | // count: 1, value: {name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] }},
|
150 | // {hash:'38d96106bc8ef3d8bd369b99bb6972702c9826d5',
|
151 | // count: 1, value: {name: 'Bob', stapler: true, friends: [] }} ]
|
152 | ```
|
153 |
|
154 | ## Legacy Browser Support
|
155 | IE <= 8 and Opera <= 11 support dropped in version 0.3.0. If you require
|
156 | legacy browser support you must either use an ES5 shim or use version 0.2.5
|
157 | of this module.
|
158 |
|
159 | ## Development
|
160 |
|
161 | ```
|
162 | git clone https://github.com/puleos/object-hash
|
163 | ```
|
164 |
|
165 | ### gulp tasks
|
166 | * `gulp watch` (default) watch files, test and lint on change/add
|
167 | * `gulp test` unit tests
|
168 | * `gulp lint` jshint
|
169 | * `gulp dist` create browser version in /dist
|
170 |
|
171 | ## License
|
172 | MIT
|
173 |
|
\ | No newline at end of file |