Skip to content

Instantly share code, notes, and snippets.

@gabalafou
Last active August 29, 2015 14:23
Show Gist options
  • Save gabalafou/c5d0f5ce3cd6c19b98a3 to your computer and use it in GitHub Desktop.
Save gabalafou/c5d0f5ce3cd6c19b98a3 to your computer and use it in GitHub Desktop.
/**
* The string hash function from Java.
*
* Borrowed from http://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
* @param {String} str
* @return {Number}
*/
function hash(str) {
var hash = 0, i, l, charCode;
if (str.length === 0)
return hash;
for (i = 0, l = str.length; i < l; i++) {
charCode = str.charCodeAt(i);
/* jshint bitwise: false */
hash = ((hash << 5) - hash) + charCode;
// Convert to 32bit integer
hash |= 0;
}
return hash;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment