Skip to content

Instantly share code, notes, and snippets.

@matt-leonardo
Created January 18, 2018 15:03
Show Gist options
  • Save matt-leonardo/aa29404b79a1e9ff471483a29ad5a003 to your computer and use it in GitHub Desktop.
Save matt-leonardo/aa29404b79a1e9ff471483a29ad5a003 to your computer and use it in GitHub Desktop.
Convert URL to Hash
// https://stackoverflow.com/questions/7616461/generate-a-hash-from-string-in-javascript-jquery
export const hashOf = (str) => {
/*jshint bitwise:false */
let i;
let l;
let hval = 0x811c9dc5;
// let hval = (seed === undefined) ? 0x811c9dc5 : seed;
const asString = true;
for (i = 0, l = str.length; i < l; i++) {
hval ^= str.charCodeAt(i);
hval += (hval << 1) + (hval << 4) + (hval << 7) + (hval << 8) + (hval << 24);
}
if (asString) {
// Convert to 8 digit hex string
return ('0000000' + (hval >>> 0).toString(16)).substr(-8);
}
return hval >>> 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment