Nodejs String Hash hashCode()

Here you can find the source of hashCode()

Method Source Code

/**/* ww w . java2  s .c o m*/
 * This function is copy & pasted from this answer:
 *
 * http://stackoverflow.com/a/15710692/3036625
 */
String.prototype.hashCode = function () {
    if (this.length == 0) return 0;
    return this.split("").reduce(function (a, b) {
        a = ((a << 5) - a) + b.charCodeAt(0);
        return a & a
    }, 0);
};

JSON.hashCode = function (obj) {
    return JSON.stringify(obj).hashCode();
};

Related

  1. hashCode()
    String.prototype.hashCode = function() {
      var hash = 0, i, chr, len;
      if (this.length === 0) return hash;
      for (i = 0, len = this.length; i < len; i++) {
        chr   = this.charCodeAt(i);
        hash  = ((hash << 5) - hash) + chr;
        hash |= 0; 
      return (hash >>> 0);
    ...
    
  2. hashCode()
    "use strict";
    String.prototype.hashCode = function() {
        var hash = 0, i, chr, len;
        if (this.length == 0) return hash;
        for (i = 0, len = this.length; i < len; i++) {
            chr   = this.charCodeAt(i);
            hash  = ((hash << 5) - hash) + chr;
            hash |= 0; 
        return hash;
    };
    
  3. hashCode()
    String.prototype.hashCode = function(){
        var hash = 0, i, char;
        if (this.length == 0) return hash;
        for (i = 0, l = this.length; i < l; i++) {
            char  = this.charCodeAt(i);
            hash  = ((hash << 5) - hash) + char;
            hash |= 0; 
        return hash;
    ...
    
  4. hashCode()
    "use strict";
    String.prototype.hashCode = function () {
      var hash = 0,
          i,
          chr,
          len;
      if (this.length === 0) return hash;
      for (i = 0, len = this.length; i < len; i++) {
        chr = this.charCodeAt(i);
    ...
    
  5. hashCode()
    String.prototype.hashCode = function() {
      var hash = 0, i, chr, len;
      if (this.length == 0) return hash;
      for (i = 0, len = this.length; i < len; i++) {
        chr   = this.charCodeAt(i);
        hash  = ((hash << 5) - hash) + chr;
        hash |= 0; 
      return hash;
    ...
    
  6. hashCode()
    String.prototype.hashCode = function(){
      var hash = 0;
      if(this.length == 0) return hash;
      for(i = 0; i < this.length; i++){
        char = this.charCodeAt(i);
        hash = ((hash<<5)-hash)+char;
        hash &= hash;
      return hash
    ...
    
  7. hashCode()
    String.prototype.hashCode = function() {
      var hash = 0,
        i, chr, len;
      if (this.length === 0) return hash;
      for (i = 0, len = this.length; i < len; i++) {
        chr = this.charCodeAt(i);
        hash = ((hash << 5) - hash) + chr;
        hash |= 0;
      return hash;
    };
    
  8. hashCode()
    String.prototype.hashCode = function () {
        var hash = 0;
        if (this.length == 0) return hash;
        for (i = 0; i < this.length; i++) {
            char = this.charCodeAt(i);
            hash = ((hash << 5) - hash) + char;
            hash = hash & hash; 
        return hash.toString();
    ...
    
  9. hashCode()
    String.prototype.hashCode = function () {
        var hash = 0, i, chr, len;
        if (this.length === 0) return hash;
        for (i = 0, len = this.length; i < len; i++) {
            chr = this.charCodeAt(i);
            hash = ((hash << 5) - hash) + chr;
            hash |= 0; 
        return Math.abs(hash); 
    ...