shortcode v0.5.0 Shortcode View Source

Documentation for Shortcode.

Link to this section Summary

Functions

Convert a shortcode to an integer.

Same as to_integer/1 but raises ArgumentError on invalid arguments.

Convert an hex encoded uuid or an integer to a shortcode with support of prefix.

Same as to_shortcode/2 but raises ArgumentError on invalid arguments.

Convert a shortcode to a uuid.

Same as to_uuid/1 but raises ArgumentError on invalid arguments.

Link to this section Functions

Specs

to_integer(binary()) :: {:ok, integer()} | :error

Convert a shortcode to an integer.

Examples

iex> Shortcode.to_integer("A")
{:ok, 36}

iex> Shortcode.to_integer("0")
{:ok, 0}

iex> Shortcode.to_integer("C8IF9cqY1HP7GGslHNYLI")
{:ok, 26867168257211004681214735068979920902}

iex> Shortcode.to_integer("prefix_C8IF9cqY1HP7GGslHNYLI")
{:ok, 26867168257211004681214735068979920902}

iex> Shortcode.to_integer(Ecto.UUID.bingenerate)
:error

iex> Shortcode.to_integer(1)
:error

Specs

to_integer!(binary()) :: integer()

Same as to_integer/1 but raises ArgumentError on invalid arguments.

Link to this function

to_shortcode(data, prefix \\ nil)

View Source

Specs

to_shortcode(Shortcode.Ecto.UUID.uuid() | non_neg_integer(), nil | binary()) ::
  {:ok, binary()} | :error

Convert an hex encoded uuid or an integer to a shortcode with support of prefix.

Examples

iex> Shortcode.to_shortcode("14366daa-c0f5-0f52-c9ec-e0a0b1e20006", "prefix")
{:ok, "prefix_C8IF9cqY1HP7GGslHNYLI"}

iex> Shortcode.to_shortcode(0)
{:ok, "0"}

iex> Shortcode.to_shortcode(61)
{:ok, "Z"}

iex> Shortcode.to_shortcode("00000000-0000-0000-0000-000000000000")
{:ok, "0"}

iex> Shortcode.to_shortcode("ffffffff-ffff-ffff-ffff-ffffffffffff")
{:ok, "7N42dgm5tFLK9N8MT7fHC7"}

iex> Shortcode.to_shortcode(Ecto.UUID.bingenerate)
:error

iex> Shortcode.to_shortcode("ffffffff-ffff-ffff-ffff-fffffffffffg")
:error

iex> Shortcode.to_shortcode("e0a0b1e20006")
:error

iex> Shortcode.to_shortcode("-1")
:error
Link to this function

to_shortcode!(data, prefix \\ nil)

View Source

Specs

to_shortcode!(any(), nil | binary()) :: binary()

Same as to_shortcode/2 but raises ArgumentError on invalid arguments.

Specs

to_uuid(binary() | any()) :: {:ok, Shortcode.Ecto.UUID.uuid()} | :error

Convert a shortcode to a uuid.

Examples

iex> Shortcode.to_uuid("0")
{:ok, "00000000-0000-0000-0000-000000000000"}

iex> Shortcode.to_uuid("C8IF9cqY1HP7GGslHNYLI")
{:ok, "14366daa-c0f5-0f52-c9ec-e0a0b1e20006"}

iex> Shortcode.to_uuid("prefix_C8IF9cqY1HP7GGslHNYLI")
{:ok, "14366daa-c0f5-0f52-c9ec-e0a0b1e20006"}

iex> Shortcode.to_uuid("7N42dgm5tFLK9N8MT7fHC8")
:error

iex> Shortcode.to_uuid(Ecto.UUID.bingenerate())
:error

iex> Shortcode.to_uuid("")
:error

Specs

to_uuid!(binary()) :: Shortcode.Ecto.UUID.uuid()

Same as to_uuid/1 but raises ArgumentError on invalid arguments.