Last active
December 19, 2015 11:09
-
-
Save ile/5946044 to your computer and use it in GitHub Desktop.
This file contains 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
DICTIONARY = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".split("") | |
encode = (i) -> | |
return DICTIONARY[0] if i is 0 | |
result = "" | |
base = DICTIONARY.length | |
while i > 0 | |
result += DICTIONARY[(i % base)] | |
i = Math.floor(i / base) | |
result.split("").reverse().join "" | |
decode = (input) -> | |
i = 0 | |
base = DICTIONARY.length | |
input.split("").forEach (c) -> | |
i = i * base + DICTIONARY.indexOf(c) | |
i |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
License: MIT License
From:
http://jsdo.it/rfkrocktk/k9Jq
http://stackoverflow.com/questions/742013/how-to-code-a-url-shortener