UNPKG

1.55 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6/**
7 * JS Implementation of MurmurHash2
8 *
9 * @author <a href="mailto:gary.court@gmail.com">Gary Court</a>
10 * @see http://github.com/garycourt/murmurhash-js
11 * @author <a href="mailto:aappleby@gmail.com">Austin Appleby</a>
12 * @see http://sites.google.com/site/murmurhash/
13 *
14 * @param {string} str ASCII only
15 * @return {string} Base 36 encoded hash result
16 */
17function murmurhash2_32_gc(str) {
18 var l = str.length;
19 var h = l;
20 var i = 0;
21 var k = void 0;
22
23 while (l >= 4) {
24 k = str.charCodeAt(i) & 0xff | (str.charCodeAt(++i) & 0xff) << 8 | (str.charCodeAt(++i) & 0xff) << 16 | (str.charCodeAt(++i) & 0xff) << 24;
25
26 k = (k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0x5bd1e995 & 0xffff) << 16);
27 k ^= k >>> 24;
28 k = (k & 0xffff) * 0x5bd1e995 + (((k >>> 16) * 0x5bd1e995 & 0xffff) << 16);
29
30 h = (h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0x5bd1e995 & 0xffff) << 16) ^ k;
31
32 l -= 4;
33 ++i;
34 } // forgive existing code
35
36 /* eslint-disable no-fallthrough */switch (l) {
37 case 3:
38 h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
39 case 2:
40 h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
41 case 1:
42 h ^= str.charCodeAt(i) & 0xff;
43 h = (h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0x5bd1e995 & 0xffff) << 16);
44 }
45 /* eslint-enable no-fallthrough */
46
47 h ^= h >>> 13;
48 h = (h & 0xffff) * 0x5bd1e995 + (((h >>> 16) * 0x5bd1e995 & 0xffff) << 16);
49 h ^= h >>> 15;
50
51 return (h >>> 0).toString(36);
52}
53
54exports.default = murmurhash2_32_gc;
\No newline at end of file