Skip to content

Instantly share code, notes, and snippets.

@jedp
Created July 23, 2012 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jedp/3166329 to your computer and use it in GitHub Desktop.
Save jedp/3166329 to your computer and use it in GitHub Desktop.
Hash to small integer
// Hash 'string' to a small number (digits default: 6)
function hash(string, digits) {
digits = digits || 6;
var m = Math.pow(10, digits+1) - 1;
var phi = Math.pow(10, digits) / 2 - 1;
var n = 0;
for (var i = 0; i < string.length; i++) {
n = (n + phi * string.charCodeAt(i)) % m;
}
return n.toString();
}
@fefogarcia
Copy link

Thank you for this!

@jedp
Copy link
Author

jedp commented Mar 13, 2023

@fefogarcia you're welcome!

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