Created
          May 23, 2019 11:50 
        
      - 
      
 - 
        
Save matchs/47593c6ce5b0a6bee7205a57f651eb81 to your computer and use it in GitHub Desktop.  
    A numeric hash function that takes a string and outputs a number
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | function hashNumber(str) { | |
| let hash = 0; | |
| if (str.length === 0) return hash; | |
| for (let i = 0; i < str.length; i += 1) { | |
| const char = str.charCodeAt(i); | |
| hash = (hash << 5) - hash + char; // eslint-disable-line | |
| hash &= hash; // eslint-disable-line | |
| } | |
| return Math.abs(hash); | |
| } | |
| // hashNumber(str) % 10 | |
| // that will give you a constant way of getting a hash integer for the same string | |
| // you can use, for instance, to hash a fingerprint string and then use this number | |
| // to decide on some sort of variant for A/B tests or staged roll-outs | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment