UNPKG

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