MCPcopy Index your code
hub / github.com/WebDevSimplified/feature-flags-sample-code / murmurhash

Function murmurhash

src/lib/murmurhash.ts:10–75  ·  view source on GitHub ↗
(key: string)

Source from the content-addressed store, hash-verified

8 */
9
10export function murmurhash(key: string) {
11 const remainder = key.length & 3
12 const bytes = key.length - remainder
13 const c1 = 0xcc9e2d51
14 const c2 = 0x1b873593
15
16 let h1 = 0
17 let i = 0
18
19 while (i < bytes) {
20 let k1 =
21 (key.charCodeAt(i) & 0xff) |
22 ((key.charCodeAt(++i) & 0xff) << 8) |
23 ((key.charCodeAt(++i) & 0xff) << 16) |
24 ((key.charCodeAt(++i) & 0xff) << 24)
25 ++i
26
27 k1 =
28 ((k1 & 0xffff) * c1 + ((((k1 >>> 16) * c1) & 0xffff) << 16)) & 0xffffffff
29 k1 = (k1 << 15) | (k1 >>> 17)
30 k1 =
31 ((k1 & 0xffff) * c2 + ((((k1 >>> 16) * c2) & 0xffff) << 16)) & 0xffffffff
32
33 h1 ^= k1
34 h1 = (h1 << 13) | (h1 >>> 19)
35 const h1b =
36 ((h1 & 0xffff) * 5 + ((((h1 >>> 16) * 5) & 0xffff) << 16)) & 0xffffffff
37 h1 = (h1b & 0xffff) + 0x6b64 + ((((h1b >>> 16) + 0xe654) & 0xffff) << 16)
38 }
39
40 let k2 = 0
41
42 switch (remainder) {
43 case 3:
44 k2 ^= (key.charCodeAt(i + 2) & 0xff) << 16
45 case 2:
46 k2 ^= (key.charCodeAt(i + 1) & 0xff) << 8
47 case 1:
48 k2 ^= key.charCodeAt(i) & 0xff
49
50 k2 =
51 ((k2 & 0xffff) * c1 + ((((k2 >>> 16) * c1) & 0xffff) << 16)) &
52 0xffffffff
53 k2 = (k2 << 15) | (k2 >>> 17)
54 k2 =
55 ((k2 & 0xffff) * c2 + ((((k2 >>> 16) * c2) & 0xffff) << 16)) &
56 0xffffffff
57 h1 ^= k2
58 }
59
60 h1 ^= key.length
61
62 h1 ^= h1 >>> 16
63 h1 =
64 ((h1 & 0xffff) * 0x85ebca6b +
65 ((((h1 >>> 16) * 0x85ebca6b) & 0xffff) << 16)) &
66 0xffffffff
67 h1 ^= h1 >>> 13

Callers 1

userIsWithinPercentageFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected