View Source ShortUUID (ShortUUID v2.1.1)

An Elixir module for handling ShortUUIDs.

This module provides functions to encode and decode between regular UUIDs and ShortUUIDs.

It can be especially useful when you want to use UUIDs, but you need them to be shorter. For example, you can use ShortUUIDs in URL shorteners or for a more user-friendly representation of UUIDs.

Inspired by bitcoin.

examples

Examples

iex> ShortUUID.encode("64d7280f-736a-4ffa-b9c0-383f43486d0b")
{:ok, "DTEETeS5R2XxjrVTZxXoJS"}

iex> ShortUUID.decode("DTEETeS5R2XxjrVTZxXoJS")
{:ok, "64d7280f-736a-4ffa-b9c0-383f43486d0b"}

Link to this section Summary

Types

A short UUID is a more compact string representation of a UUID. It encodes the exact same information but uses a larger character set. This results in a shorter string for the same information.

A UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify some object or entity on the internet. In its canonical form, a UUID is represented by 32 lowercase hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and 4 hyphens).

Functions

Decodes the given ShortUUID back into a UUID.

Decodes the given ShortUUID back into a UUID.

Encodes the given UUID into a ShortUUID.

Encodes the given UUID into a ShortUUID.

Link to this section Types

@type short_uuid() :: binary()

A short UUID is a more compact string representation of a UUID. It encodes the exact same information but uses a larger character set. This results in a shorter string for the same information.

@type uuid() :: binary()

A UUID (Universally Unique Identifier) is a 128-bit number used to uniquely identify some object or entity on the internet. In its canonical form, a UUID is represented by 32 lowercase hexadecimal digits, displayed in five groups separated by hyphens, in the form 8-4-4-4-12 for a total of 36 characters (32 alphanumeric characters and 4 hyphens).

Link to this section Functions

@spec decode(short_uuid()) :: {:ok, uuid()} | {:error, :invalid_uuid}

Decodes the given ShortUUID back into a UUID.

The function returns a tuple {:ok, uuid} if decoding is successful. In case of invalid input ShortUUID, the function returns {:error, :invalid_uuid}.

examples

Examples

iex> ShortUUID.decode("DTEETeS5R2XxjrVTZxXoJS")
{:ok, "64d7280f-736a-4ffa-b9c0-383f43486d0b"}

iex> ShortUUID.decode("DTEETeS5R2XxjrVTZxXoJS123")
{:error, :invalid_uuid}

iex> ShortUUID.decode("InvalidShortUUID")
{:error, :invalid_uuid}
Link to this function

decode!(input)

View Source (since 2.1.0)
@spec decode!(short_uuid()) :: uuid()

Decodes the given ShortUUID back into a UUID.

This function works similarly to decode/1, but instead of returning an error tuple, it raises ArgumentError in case of an invalid ShortUUID.

examples

Examples

iex> ShortUUID.decode!("DTEETeS5R2XxjrVTZxXoJS")
"64d7280f-736a-4ffa-b9c0-383f43486d0b"
@spec encode(uuid()) :: {:ok, short_uuid()} | {:error, :invalid_uuid}

Encodes the given UUID into a ShortUUID.

The function returns a tuple {:ok, short_uuid} if encoding is successful. In case of invalid input UUID, the function returns {:error, :invalid_uuid}.

examples

Examples

iex> ShortUUID.encode("64d7280f-736a-4ffa-b9c0-383f43486d0b")
{:ok, "DTEETeS5R2XxjrVTZxXoJS"}

iex> ShortUUID.encode("invalid-uuid-here")
{:error, :invalid_uuid}
Link to this function

encode!(input)

View Source (since 2.1.0)
@spec encode!(uuid()) :: short_uuid()

Encodes the given UUID into a ShortUUID.

This function works similarly to encode/1, but instead of returning an error tuple, it raises ArgumentError in case of an invalid UUID.

examples

Examples

iex> ShortUUID.encode!("64d7280f-736a-4ffa-b9c0-383f43486d0b")
"DTEETeS5R2XxjrVTZxXoJS"