Created
October 26, 2016 17:11
-
-
Save thecodeite/72f2974d219607b085e8cd3633c786fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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