ShortUUID (ShortUUID v3.0.0) View Source

ShortUUID - generate concise, unambiguous, URL-safe UUIDs.

Installation

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

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

Usage

The encode/1 function translates UUIDs to base57 using lowercase and uppercase letters, as well as digits, while ensuring that similar-looking characters such as 'l', '1', 'I', 'O', and '0' are avoided.

  iex> ShortUUID.encode("ed7ba470-8e54-465e-825c-99712043e01c")
  {:ok, "kGSmvLEEuzjcFZTChXwZpz"}

The decode/1 function will turn the ShortUUID back into a regular UUID

  iex> ShortUUID.decode("kGSmvLEEuzjcFZTChXwZpz")
  {:ok, "ed7ba470-8e54-465e-825c-99712043e01c"}

ShortUUID strives to do one thing well, encode UUIDs. To generate UUIDs use any UUID library of your choice.

Some options: Ecto Elixir UUID Erlang UUID

Notes

ShortUUID supports the following input formats:

  • 2a162ee5-02f4-4701-9e87-72762cbce5e2
  • 2a162ee502f447019e8772762cbce5e2

Letter case is not relevant.

Since version v2.1.0, ShortUUID also supports the encoding of binary UUIDs.

iex> ShortUUID.encode!(<<0xFA, 0x62, 0xAF, 0x80, 0xA8, 0x61, 0x45, 0x6C, 0xAB, 0x77, 0xD5, 0x67, 0x7E, 0x2E, 0x8B, 0xA8>>)
"nZJHSqgNVBBSX2h6sRUQuP"

Using ShortUUID with Ecto

If you want to use ShortUUIDs with Ecto, you can explore the ecto_shortuuid library.

Acknowledgments

This project was inspired by skorokithakis/shortuuid.

Link to this section Summary

Functions

Decodes a ShortUUID string into a UUID.

Decodes a ShortUUID string into a UUID.

Encodes a UUID into a ShortUUID string.

Encodes a UUID into a ShortUUID string.

Link to this section Functions

Specs

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

Decodes a ShortUUID string into a UUID.

Specs

decode!(String.t()) :: String.t() | no_return()

Decodes a ShortUUID string into a UUID.

Raises an ArgumentError if the ShortUUID is invalid.

Specs

encode(binary()) :: {:ok, String.t()} | {:error, String.t()}

Encodes a UUID into a ShortUUID string.

Specs

encode!(binary()) :: String.t() | no_return()

Encodes a UUID into a ShortUUID string.

Raises an ArgumentError if the UUID is invalid.