hashcode.js 830 B

1234567891011121314151617181920212223
  1. /* eslint-disable no-bitwise */
  2. /**
  3. * Return a simple hash code from a string.
  4. * Source from: https://github.com/sindresorhus/fnv1a/blob/master/index.js#L25
  5. */
  6. export function hashcode(str) {
  7. let hash = 2166136261;
  8. let isUnicoded = false;
  9. let string = str;
  10. for (let i = 0, ii = string.length; i < ii; i += 1) {
  11. let characterCode = string.charCodeAt(i);
  12. // Non-ASCII characters trigger the Unicode escape logic
  13. if (characterCode > 0x7f && !isUnicoded) {
  14. string = unescape(encodeURIComponent(string));
  15. characterCode = string.charCodeAt(i);
  16. isUnicoded = true;
  17. }
  18. hash ^= characterCode;
  19. hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24);
  20. }
  21. return hash >>> 0;
  22. }
  23. //# sourceMappingURL=hashcode.js.map