UNPKG

1.48 kBJavaScriptView Raw
1"use strict";
2
3exports.__esModule = true;
4exports.default = void 0;
5
6/* eslint-disable prefer-destructuring, no-bitwise, default-case */
7
8/**
9 * murmurhash2 via https://gist.github.com/raycmorgan/588423
10 */
11function UInt32(str, pos) {
12 return str.charCodeAt(pos++) + (str.charCodeAt(pos++) << 8) + (str.charCodeAt(pos++) << 16) + (str.charCodeAt(pos) << 24);
13}
14
15function UInt16(str, pos) {
16 return str.charCodeAt(pos++) + (str.charCodeAt(pos++) << 8);
17}
18
19function Umul32(n, m) {
20 n |= 0;
21 m |= 0;
22 const nlo = n & 0xffff;
23 const nhi = n >>> 16;
24 const res = nlo * m + ((nhi * m & 0xffff) << 16) | 0;
25 return res;
26}
27
28function doHash(str, seed = 0) {
29 const m = 0x5bd1e995;
30 const r = 24;
31 let h = seed ^ str.length;
32 let length = str.length;
33 let currentIndex = 0;
34
35 while (length >= 4) {
36 let k = UInt32(str, currentIndex);
37 k = Umul32(k, m);
38 k ^= k >>> r;
39 k = Umul32(k, m);
40 h = Umul32(h, m);
41 h ^= k;
42 currentIndex += 4;
43 length -= 4;
44 }
45
46 switch (length) {
47 case 3:
48 h ^= UInt16(str, currentIndex);
49 h ^= str.charCodeAt(currentIndex + 2) << 16;
50 h = Umul32(h, m);
51 break;
52
53 case 2:
54 h ^= UInt16(str, currentIndex);
55 h = Umul32(h, m);
56 break;
57
58 case 1:
59 h ^= str.charCodeAt(currentIndex);
60 h = Umul32(h, m);
61 break;
62 }
63
64 h ^= h >>> 13;
65 h = Umul32(h, m);
66 h ^= h >>> 15;
67 return h >>> 0;
68}
69
70var _default = code => doHash(code).toString(36);
71
72exports.default = _default;
\No newline at end of file