You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StringUtils.js 593B

123456789101112131415161718192021222324
  1. /**
  2. * Implements a simple hash code for a string (see
  3. * https://en.wikipedia.org/wiki/Java_hashCode()).
  4. *
  5. * @param {string} The string to return a hash of.
  6. * @return {Number} the integer hash code of the string.
  7. */
  8. function integerHash(string) {
  9. if (!string) {
  10. return 0;
  11. }
  12. let char, hash = 0, i;
  13. for (i = 0; i < string.length; i++) {
  14. char = string.charCodeAt(i);
  15. hash += char * Math.pow(31, string.length - 1 - i);
  16. hash = Math.abs(hash | 0); // eslint-disable-line no-bitwise
  17. }
  18. return hash;
  19. }
  20. module.exports = { integerHash };