Skip to content

Instantly share code, notes, and snippets.

@peanutbother
Last active April 10, 2018 15:10
Show Gist options
  • Save peanutbother/970e40b56317785ec4089585aaff0047 to your computer and use it in GitHub Desktop.
Save peanutbother/970e40b56317785ec4089585aaff0047 to your computer and use it in GitHub Desktop.
hash string in format `#HASH~HASH_firstChar.HASH_lastChar`
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))}`};
@peanutbother
Copy link
Author

@peanutbother
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment