Skip to content

Instantly share code, notes, and snippets.

@SethVandebrooke
Created July 17, 2018 17:47
Show Gist options
  • Save SethVandebrooke/015cca79bbc2d1b5a4802daa1025d467 to your computer and use it in GitHub Desktop.
Save SethVandebrooke/015cca79bbc2d1b5a4802daa1025d467 to your computer and use it in GitHub Desktop.
Using base conversion to compress number values
function compress(z) {
z = z.toString();
var a = [];
z.split("").forEach((e,i)=>{a[i] = parseInt(e);});
var u = "0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM!?";
var x = u.substring(0,Math.max(...a)+1);
var s = x.length;
var d = u.length;
var v = 0;
var n = z.length;
for (var i = 0; i < n; i++) {
v = v * s + x.indexOf(z.charAt(i));
}
if (v < 0) return 0;
var r = v % d;
var c = u.charAt(r);
var q = Math.floor(v / d);
while (q) {
r = q % d; q = Math.floor(q / d); c = u.charAt(r) + c;
}
return c;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment