Created
June 25, 2018 06:02
-
-
Save mustafaturan/87b22d2408d95a079610d1eac4edd0ce to your computer and use it in GitHub Desktop.
Base62 in Elixir
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
defmodule Base62 do | |
@moduledoc """ | |
Base62 encoder | |
Copied from: https://github.com/otobus/event_bus/commit/956ffd93d854fb3a721aa7763c3da509cffedd41#diff-31f9094877d525a1b8387d4135042006R1 | |
""" | |
@mapping '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' | |
@doc """ | |
Converts given integer to base62 | |
""" | |
@spec encode(integer()) :: String.t() | |
def encode(num) when num < 62 do | |
<< Enum.at(@mapping, num) >> | |
end | |
def encode(num) do | |
encode(div(num, 62)) <> encode(rem(num, 62)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment