1 | 'use strict';
|
2 |
|
3 | function pad (hash, len) {
|
4 | while (hash.length < len) {
|
5 | hash = '0' + hash;
|
6 | }
|
7 | return hash;
|
8 | }
|
9 |
|
10 | function fold (hash, text) {
|
11 | var i;
|
12 | var chr;
|
13 | var len;
|
14 | if (text.length === 0) {
|
15 | return hash;
|
16 | }
|
17 | for (i = 0, len = text.length; i < len; i++) {
|
18 | chr = text.charCodeAt(i);
|
19 | hash = ((hash << 5) - hash) + chr;
|
20 | hash |= 0;
|
21 | }
|
22 | return hash < 0 ? hash * -2 : hash;
|
23 | }
|
24 |
|
25 | function foldObject (hash, o) {
|
26 | return Object.keys(o).reduce(foldKey, hash);
|
27 | function foldKey (hash, key) {
|
28 | return foldValue(hash, o[key], key);
|
29 | }
|
30 | }
|
31 |
|
32 | function foldValue (input, value, key) {
|
33 | var hash = fold(fold(fold(input, key), toString(value)), typeof value);
|
34 | if (value === null) {
|
35 | return fold(hash, 'null');
|
36 | }
|
37 | if (value === undefined) {
|
38 | return fold(hash, 'undefined');
|
39 | }
|
40 | if (typeof value === 'object') {
|
41 | return foldObject(hash, value);
|
42 | }
|
43 | return fold(hash, value.toString());
|
44 | }
|
45 |
|
46 | function toString (o) {
|
47 | return Object.prototype.toString.call(o);
|
48 | }
|
49 |
|
50 | function sum (o) {
|
51 | return pad(foldValue(0, o, '').toString(16), 8);
|
52 | }
|
53 |
|
54 | module.exports = sum;
|