Skip to content

Instantly share code, notes, and snippets.

@thecodeite
Created October 26, 2016 17:11
Show Gist options
  • Save thecodeite/72f2974d219607b085e8cd3633c786fa to your computer and use it in GitHub Desktop.
Save thecodeite/72f2974d219607b085e8cd3633c786fa to your computer and use it in GitHub Desktop.
const dictionary = '0123456789abcdefghijklmnopqrstuvwxyz'.split('')
function toBase36 (number) {
const prefix = number < 36 ? '' : toBase36(Math.floor(number / 36))
return prefix + dictionary[number % 36]
}
function fromBase36 (b36) {
let val = dictionary.indexOf(b36[b36.length - 1])
if (val === -1) return NaN
if (b36.length > 1) {
val += fromBase36(b36.slice(0, -1)) * 36
}
return val
}
module.exports = {toBase36, fromBase36}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment