hash.browser.cjs.js 1.7 KB

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