Last active
October 7, 2017 00:09
-
-
Save OneCent01/c848403cd7a25c0bed2d56700b5e9519 to your computer and use it in GitHub Desktop.
Implementation of a JS hash function found here: http://werxltd.com/wp/2010/05/13/javascript-implementation-of-javas-string-hashcode-method/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.hashCode = function(max){ | |
var hash = 0; | |
if (!this.length) return hash; | |
for (i = 0; i < this.length; i++) { | |
char = this.charCodeAt(i); | |
hash = ((hash<<5)-hash)+char; | |
hash = hash & hash; // Convert to 32bit integer | |
} | |
return Math.abs(max?hash%max:hash); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment