Skip to content

Instantly share code, notes, and snippets.

@alexkirmse
Created December 21, 2011 05:04
Show Gist options
  • Save alexkirmse/1504665 to your computer and use it in GitHub Desktop.
Save alexkirmse/1504665 to your computer and use it in GitHub Desktop.
Bijective encoding function in JavaScript for Base62
// Simple port of a ruby bijective encode
// Ported from this Ruby example https://gist.github.com/1073996
function bijectiveEncode(i){
var alphaNumArray = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split('');
if (i == 0){
return alphaNumArray[0];
}
s = [];
var base = alphaNumArray.length;
while (i > 0){
console.log(i);
s.push(alphaNumArray[i % base]);
i = Math.floor(i / base);
}
var enc = s.reverse();
return enc.join('');
}
@peteristhegreat
Copy link

Thanks for posting. I found this, then I found this link that has the encode and decode together. https://github.com/delight-im/ShortURL/blob/master/JavaScript/ShortURL.js

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