Skip to content

Instantly share code, notes, and snippets.

@winton
Created January 7, 2019 22:31
Show Gist options
  • Select an option

  • Save winton/6950d13231f274d2eff0c56d6c39f64d to your computer and use it in GitHub Desktop.

Select an option

Save winton/6950d13231f274d2eff0c56d6c39f64d to your computer and use it in GitHub Desktop.
let alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("")
let base = alphabet.length
function decode(code) {
let c, i
i = 0
for (let j = 0, len = code.length; j < len; j++) {
c = code[j]
i = i * base + alphabet.indexOf(c)
}
return i
}
function encode(id) {
let s
if (id === 0) {
return { code: alphabet[0] }
}
s = ""
while (id > 0) {
s += alphabet[id % base]
id = parseInt(id / base, 10)
}
return s.split("").reverse().join("")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment