ShortUUID v2.0.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.0"}]
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.

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

Link to this function

decode(string) View Source
decode(String.t()) :: {:ok, String.t()} | {:error, String.t()}

Decode a ShortUUID.

Examples

iex> ShortUUID.decode("keATfB8JP2ggT7U9JZrpV9")
{:ok, "2a162ee5-02f4-4701-9e87-72762cbce5e2"}

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"
Link to this function

encode(uuid) View Source
encode(String.t()) :: {:ok, String.t()} | {:error, String.t()}

Encode a UUID to ShortUUID.

Examples

iex> ShortUUID.encode("2a162ee5-02f4-4701-9e87-72762cbce5e2")
{:ok, "keATfB8JP2ggT7U9JZrpV9"}

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"