ShortUUID v2.1.1 ShortUUID View Source
ShortUUID - generate concise, unambiguous, URL-safe UUIDs
Installation
Add ShortUUID to your list of dependencies in mix.exs
:
def deps do
[{:shortuuid, "~> 2.1"}]
end
encode/1
will translate UUIDs to base57 using lowercase and uppercase letters
and digits while avoiding similar-looking characters such as l, 1, I, O and 0.
Typical usage
ShortUUID strives to do one thing well, encode UUIDs. To generate the UUIDs use any UUID library you like. Some of options out there are Ecto, Elixir UUID and Erlang UUID.
Notes
The output is padded to a length of 22 with the first character of the alphabet (2).
iex> ShortUUID.encode!("00000000-0000-0000-0000-000000000000")
"2222222222222222222222"
iex> ShortUUID.encode!("00000001-0001-0001-0001-000000000001")
"UD6ibhr3V4YXvriP822222"
iex> ShortUUID.decode!("UD6ibhr3V4YXvriP822222")
"00000001-0001-0001-0001-000000000001"
The input format is quite flexible as any non base16 chars are stripped from the input which is then downcased, however only the following formats are covered by tests thus guaranteed to work.
2a162ee5-02f4-4701-9e87-72762cbce5e2
2a162ee502f447019e8772762cbce5e2
{2a162ee5-02f4-4701-9e87-72762cbce5e2}
{2a162ee502f447019e8772762cbce5e2}
Letter case is not relevant.
Also supported since v2.1.0
is the encoding of binary UUIDs
iex> ShortUUID.encode!(<<0xFA, 0x62, 0xAF, 0x80, 0xA8, 0x61, 0x45, 0x6C, 0xAB, 0x77, 0xD5, 0x67, 0x7E, 0x2E, 0x8B, 0xA8>>)
"PuQURs6h2XSBBVNgqSHJZn"
Using ShortUUID with Ecto
If you would like to use ShortUUIDs with Ecto check out ecto_shortuuid.
Acknowledgments
This project was inspired by skorokithakis/shortuuid.
Link to this section Summary
Functions
Decode a ShortUUID
Decode a ShortUUID
Encode a UUID to ShortUUID
Encode a UUID to ShortUUID
Link to this section Functions
decode(input) View Source
Decode a ShortUUID.
Examples
iex> ShortUUID.decode("keATfB8JP2ggT7U9JZrpV9")
{:ok, "2a162ee5-02f4-4701-9e87-72762cbce5e2"}
decode!(string) View Source
Decode a ShortUUID.
Similar to decode/1
but raises an ArgumentError if the encoded UUID is invalid.
Examples
iex> ShortUUID.decode!("keATfB8JP2ggT7U9JZrpV9")
"2a162ee5-02f4-4701-9e87-72762cbce5e2"
encode(uuid) View Source
Encode a UUID to ShortUUID.
Examples
iex> ShortUUID.encode("2a162ee5-02f4-4701-9e87-72762cbce5e2")
{:ok, "keATfB8JP2ggT7U9JZrpV9"}
encode!(uuid) View Source
Encode a UUID to ShortUUID.
Similar to encode/1
but raises an ArgumentError if it cannot process the UUID.
Examples
iex> ShortUUID.encode!("2a162ee5-02f4-4701-9e87-72762cbce5e2")
"keATfB8JP2ggT7U9JZrpV9"