Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
Size: Mime:
/**
 * Generate a 32bit integer hash for a given string
 * Sources:
 *   - http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
 *   - https://stackoverflow.com/questions/194846/is-there-any-kind-of-hash-code-function-in-javascript/8076436#8076436
 *   - https://gist.github.com/hyamamoto/fd435505d29ebfa3d9716fd2be8d42f0
 *   - https://en.wikipedia.org/wiki/Java_hashCode()
 */
export default str => {
  let hash = 0;
  if (str.length === 0) return hash;

  for (let i = 0; i < str.length; i += 1) {
    const char = str.charCodeAt(i);
    // eslint-disable-next-line no-bitwise
    hash = (hash << 5) - hash + char;
    // eslint-disable-next-line no-bitwise
    hash &= hash; // Convert to 32bit integer
  }

  return hash;
};