/** * Returns a hash code for a string, the hash value of the empty string is zero. * (Compatible with Java's String.hashCode()) * @param {string} string a string * @return {number} a hash code value for the given string. */ function stringToHashCode(string: string) { let hash = 0; for (let i = 0; i < string.length; i++) { // the "| 0" part forces hash to be a 32-bit number, optimizing for speed in JS engines hash = ((Math.imul ? Math.imul(31, hash) : (hash << 5) - hash) + string.charCodeAt(i)) | 0; } return hash; } export default stringToHashCode;