ShortUUID (ShortUUID v4.3.0)

Copy Markdown View Source

ShortUUID is a module for encoding and decoding UUIDs using a base57 alphabet.

Functions

  • encode/1 - Encodes a UUID into a shorter string using base57 alphabet
  • encode!/1 - Same as encode/1 but raises an error on failure
  • decode/1 - Decodes a shortened string back into a UUID
  • decode!/1 - Same as decode/1 but raises an error on failure

Example

iex> ShortUUID.encode("550e8400-e29b-41d4-a716-446655440000")
{:ok, "H9cNmGXLEc8NWcZzSThA9S"}

iex> ShortUUID.decode("H9cNmGXLEc8NWcZzSThA9S")
{:ok, "550e8400-e29b-41d4-a716-446655440000"}

For custom alphabets and more options, see ShortUUID.Builder. To implement your own compatible ShortUUID module, use ShortUUID.Behaviour.

Summary

Functions

Decodes a 22-character short ID back into a lowercase, hyphenated UUID string.

Decodes a short ID like decode/1, but returns the UUID directly and raises an ArgumentError on invalid input.

Encodes a UUID string into a 22-character short ID.

Encodes a UUID string like encode/1, but returns the short ID directly and raises an ArgumentError on invalid input.

Functions

decode(string)

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

Decodes a 22-character short ID back into a lowercase, hyphenated UUID string.

Short IDs are case-sensitive and must use exactly this module's alphabet (57 characters: "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"). Returns {:ok, uuid} or {:error, "Invalid input"}.

decode!(string)

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

Decodes a short ID like decode/1, but returns the UUID directly and raises an ArgumentError on invalid input.

encode(uuid)

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

Encodes a UUID string into a 22-character short ID.

Accepts UUIDs with or without hyphens, in curly braces or with a urn:uuid: prefix, in any letter case. The alphabet has 57 characters: "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz".

Returns {:ok, short_id} or {:error, "Invalid UUID"}.

encode!(uuid)

@spec encode!(String.t()) :: String.t() | no_return()

Encodes a UUID string like encode/1, but returns the short ID directly and raises an ArgumentError on invalid input.