UNPKG

5.31 kBMarkdownView Raw
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
7Generate hashes from objects and values in node and the browser. Uses node.js
8crypo 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```
15var hash = require('object-hash');
16```
17## hash(value, options);
18Generate 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);
24Hash using the sha1 algorithm.
25
26*Sugar method, equivalent to hash(value, {algorithm: 'sha1'})*
27
28## hash.keys(value);
29Hash object keys using the sha1 algorithm, values ignored.
30
31*Sugar method, equivalent to hash(value, {excludeValues: true})*
32
33## hash.MD5(value);
34Hash using the md5 algorithm.
35
36*Sugar method, equivalent to hash(value, {algorithm: 'md5'})*
37
38## hash.keysMD5(value);
39Hash 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);
44Create a new HashTable instance. Hashing options are supported and applied to all values
45added to the table.
46
47## hashTable.add(value1, value2, ...);
48Add an object to the hash table. Supports n parameters or an array of values to be
49added to the table.
50
51*Note: if you wish to evaluate an array as a single table entry
52you must wrap it first `{[1,2,3,4]}` otherwise each element will be added to the
53table separately.*
54
55## hashTable.getValue(hashKey);
56Retrive the objects value from the table by hash key. If there is no matching entry
57returns undefined.
58
59## hashTable.getCount(hashKey);
60Retrieve a counter representing the number of times an object was added to
61the table. Returns 0 if a matching key is not found.
62
63## hashTable.hasKey(hashKey);
64Returns true if the specified hash is in the hash table otherwise false.
65
66## hashTable.toArray();
67Returns 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();
77Clears the contents of the hash table.
78
79## Installation
80
81node:
82```
83npm install object-hash
84```
85
86browser: */dist/object_hash.js*
87```
88<script src="object_hash.js" type="text/javascript"></script>
89```
90
91## Example usage
92```js
93var hash = require('object-hash');
94
95var peter = {name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
96var michael = {name: 'Michael', stapler: false, friends: ['Peter', 'Samir'] };
97var bob = {name: 'Bob', stapler: true, friends: [] };
98
99/***
100 * sha1 hex encoding (default)
101 */
102hash(peter);
103// 14fa461bf4b98155e82adc86532938553b4d33a9
104hash(michael);
105// 4b2b30e27699979ce46714253bc2213010db039c
106hash(bob);
107// 38d96106bc8ef3d8bd369b99bb6972702c9826d5
108
109/***
110 * hash object keys, values ignored
111 */
112hash(peter, { excludeValues: true });
113// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
114hash(michael, { excludeValues: true });
115// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
116hash.keys(bob);
117// 48f370a772c7496f6c9d2e6d92e920c87dd00a5c
118
119/***
120 * md5 base64 encoding
121 */
122hash(peter, { algorithm: 'md5', encoding: 'base64' });
123// 6rkWaaDiG3NynWw4svGH7g==
124hash(michael, { algorithm: 'md5', encoding: 'base64' });
125// djXaWpuWVJeOF8Sb6SFFNg==
126hash(bob, { algorithm: 'md5', encoding: 'base64' });
127// lFzkw/IJ8/12jZI0rQeS3w==
128
129/***
130 * HashTable example
131 */
132var hashTable = new hash.HashTable();
133var peterHash = hash(peter);
134
135hashTable.add(peter, michael, bob);
136hashTable.getValue(peterHash);
137// {name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] };
138hashTable.getCount(peterHash);
139// 1
140hashTable.add({name: 'Peter', stapler: false, friends: ['Joanna', 'Michael', 'Samir'] });
141hashTable.getCount(peterHash);
142// 2
143hashTable.hasKey(peterHash);
144// true
145hashTable.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
155IE <= 8 and Opera <= 11 support dropped in version 0.3.0. If you require
156legacy browser support you must either use an ES5 shim or use version 0.2.5
157of this module.
158
159## Development
160
161```
162git 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
172MIT
173
\No newline at end of file