Last active
April 10, 2018 15:10
-
-
Save peanutbother/970e40b56317785ec4089585aaff0047 to your computer and use it in GitHub Desktop.
hash string in format `#HASH~HASH_firstChar.HASH_lastChar`
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
const hash= (str = "") => { | |
let _hash = (str) => | |
str.split('').reduce((prevHash, currVal) => | |
(((prevHash << 5) - prevHash) + currVal.charCodeAt(0))|0, 0); | |
return `#${str.length > 3?_hash(str.slice(1,-1)):_hash(str)}~${_hash(str.charAt(0))}.${_hash(str.charAt(str.length-1))}` | |
} | |
// ~ minified ~ | |
const hash=(t="")=>{let e=t=>t.split("").reduce((t,e)=>(t<<5)-t+e.charCodeAt(0)|0,0);return`#${t.length>3?e(t.slice(1,-1)):e(t)}~${e(t.charAt(0))}.${e(t.charAt(t.length-1))}`}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
_hash
function from https://stackoverflow.com/a/34842797/3793309