hash.esm.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* eslint-disable */
  2. // Inspired by https://github.com/garycourt/murmurhash-js
  3. // Ported from https://github.com/aappleby/smhasher/blob/61a0530f28277f2e850bfc39600ce61d02b518de/src/MurmurHash2.cpp#L37-L86
  4. function murmur2(str) {
  5. // 'm' and 'r' are mixing constants generated offline.
  6. // They're not really 'magic', they just happen to work well.
  7. // const m = 0x5bd1e995;
  8. // const r = 24;
  9. // Initialize the hash
  10. var h = 0; // Mix 4 bytes at a time into the hash
  11. var k,
  12. i = 0,
  13. len = str.length;
  14. for (; len >= 4; ++i, len -= 4) {
  15. k = str.charCodeAt(i) & 0xff | (str.charCodeAt(++i) & 0xff) << 8 | (str.charCodeAt(++i) & 0xff) << 16 | (str.charCodeAt(++i) & 0xff) << 24;
  16. k =
  17. /* Math.imul(k, m): */
  18. (k & 0xffff) * 0x5bd1e995 + ((k >>> 16) * 0xe995 << 16);
  19. k ^=
  20. /* k >>> r: */
  21. k >>> 24;
  22. h =
  23. /* Math.imul(k, m): */
  24. (k & 0xffff) * 0x5bd1e995 + ((k >>> 16) * 0xe995 << 16) ^
  25. /* Math.imul(h, m): */
  26. (h & 0xffff) * 0x5bd1e995 + ((h >>> 16) * 0xe995 << 16);
  27. } // Handle the last few bytes of the input array
  28. switch (len) {
  29. case 3:
  30. h ^= (str.charCodeAt(i + 2) & 0xff) << 16;
  31. case 2:
  32. h ^= (str.charCodeAt(i + 1) & 0xff) << 8;
  33. case 1:
  34. h ^= str.charCodeAt(i) & 0xff;
  35. h =
  36. /* Math.imul(h, m): */
  37. (h & 0xffff) * 0x5bd1e995 + ((h >>> 16) * 0xe995 << 16);
  38. } // Do a few final mixes of the hash to ensure the last few
  39. // bytes are well-incorporated.
  40. h ^= h >>> 13;
  41. h =
  42. /* Math.imul(h, m): */
  43. (h & 0xffff) * 0x5bd1e995 + ((h >>> 16) * 0xe995 << 16);
  44. return ((h ^ h >>> 15) >>> 0).toString(36);
  45. }
  46. export default murmur2;