View Source ShortUUID (ShortUUID v3.0.0)

Encode and decode UUIDs into a shorter Base58 representation.

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

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

prefixed-ids-ecto

Prefixed IDs (Ecto)

With Ecto installed, ShortUUID.Prefixed provides Stripe-style prefixed IDs as an Ecto parameterized type. UUIDs are stored as-is in the database, prefixed ShortUUIDs are used in your application:

defmodule MyApp.User do
  use ShortUUID.Prefixed.Schema, prefix: "usr"

  schema "users" do
  end
end

user = Repo.get!(User, "usr_F6tzXELwufrXBFtFTKsUvc")
user.id #=> "usr_F6tzXELwufrXBFtFTKsUvc"

See ShortUUID.Prefixed docs for helpers and manual schema setup.

Link to this section Summary

Types

A Base58-encoded UUID, e.g. "DTEETeS5R2XxjrVTZxXoJS".

A standard UUID string, e.g. "64d7280f-736a-4ffa-b9c0-383f43486d0b".

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 Base58-encoded UUID, e.g. "DTEETeS5R2XxjrVTZxXoJS".

@type uuid() :: binary()

A standard UUID string, e.g. "64d7280f-736a-4ffa-b9c0-383f43486d0b".

Link to this section Functions

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

Decodes the given ShortUUID back into a UUID.

Returns {:ok, uuid} on success, :error on invalid input.

examples

Examples

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

iex> ShortUUID.decode("DTEETeS5R2XxjrVTZxXoJS123")
:error

iex> ShortUUID.decode("InvalidShortUUID")
:error
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.

Raises ArgumentError on invalid input.

examples

Examples

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

Encodes the given UUID into a ShortUUID.

Returns {:ok, short_uuid} on success, :error on invalid input.

examples

Examples

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

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

encode!(input)

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

Encodes the given UUID into a ShortUUID.

Raises ArgumentError on invalid input.

examples

Examples

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