Skip to content

Instantly share code, notes, and snippets.

@stvkoch
Last active July 4, 2019 15:02
Show Gist options
  • Save stvkoch/8f4a61d989e1cac51ce7d938e2840df6 to your computer and use it in GitHub Desktop.
Save stvkoch/8f4a61d989e1cac51ce7d938e2840df6 to your computer and use it in GitHub Desktop.
generateHashFromObject
const generateHashFromObject = (obj) =>
Object.entries(obj).reduce((hash, [key, str])=> {
let result = hash;
if (!str || !key) return hash;
if (!str.charCodeAt && !isNaN(str)) {
result = (result * 33) ^ str;
}
if (!key.charCodeAt && !isNaN(key)) {
result = (result * 33) ^ key;
}
let i = str.length;
if (str.charCodeAt)
while (i) {
result = (result * 33) ^ str.charCodeAt(--i);
}
i = key.length;
if (key.charCodeAt)
while (i) {
result = (result * 33) ^ key.charCodeAt(--i);
}
return result >>> 0;
},
5381,
).toString(32);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment