Build StatusCoverage Status

ShortUUID

ShortUUID is a simple Elixir library that generates concise, unambiguous, URL-safe UUIDs.

Often, one needs to use non-sequential IDs in places where users will see them, but the IDs must be as concise and easy to use as possible. ShortUUID solves this problem by translating regular UUIDs to base57 using lowercase and uppercase letters and digits, and removing similar-looking characters such as l, 1, I, O and 0.

Inspired by shortuuid.

Note: As long as the they use the same alphabet (23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz) different shortuuid implementations should be compatible, however there is no official standard afaik so I would strongly advise to do your own research and compatiblity testing if you're doing any sort of interop between different libraries.

Unlike other shortuuid libraries this one does not generate UUIDs as this would break with the single task principle and also add potential uneeded dependencies. You can feed any valid UUID into ShortUUID.encode/1. Some of the libraries you can use to generate UUIDs are Elixir UUID, Erlang UUID and also Ecto as it can generate version 4 UUIDs.

ShortUUID supports the most common formats of UUIDs:

  "2A162EE5-02F4-4701-9E87-72762CBCE5E2"
  "2a162ee5-02f4-4701-9e87-72762cbce5e2"
  "2a162ee502f447019e8772762cbce5e2"
  "{2a162ee5-02f4-4701-9e87-72762cbce5e2}"
  "{2a162ee502f447019e8772762cbce5e2}"

The input string is also downcased before processing so the letter case doesn't matter.

Installation

Add ShortUUID to your list of dependencies in mix.exs:

  def deps do
    [
      {:shortuuid, "~> 2.0"}
    ]
  end

Examples

iex> "f98e80e7-9923-4173-8408-98f8254912ad" |> ShortUUID.encode
{:ok, "EwQd7sRtDbyyB6QRSWAtQn"}

iex> "f98e80e7-9923-4173-8408-98f8254912ad" |> ShortUUID.encode!
"EwQd7sRtDbyyB6QRSWAtQn"

iex> "EwQd7sRtDbyyB6QRSWAtQn" |> ShortUUID.decode
{:ok, "f98e80e7-9923-4173-8408-98f8254912ad"}

iex> "EwQd7sRtDbyyB6QRSWAtQn" |> ShortUUID.decode!
"f98e80e7-9923-4173-8408-98f8254912ad"

Documentation

Look up the full documentation at https://hexdocs.pm/shortuuid.