LemonCore.UUID (lemon_core v0.1.0)

View Source

Minimal RFC 9562 UUID generation.

Vendored so the umbrella does not depend on the unmaintained :uuid package. Output is the canonical 8-4-4-12 lowercase hex string, byte-compatible with what UUID.uuid4/0 produced.

Two versions are available:

  • uuid4/0 — 122 random bits. The default for identifiers that must not leak creation time.
  • uuid7/0 — 48-bit millisecond timestamp followed by 74 random bits. Lexicographically sortable by creation time, which makes it the better choice for database keys. Sortable to the millisecond: the bits after the timestamp are entirely random, so UUIDs minted within the same millisecond have no defined order relative to each other. Do not read sort order as insertion order.

Examples

iex> uuid = LemonCore.UUID.uuid4()
iex> String.length(uuid)
36
iex> LemonCore.UUID.version(uuid)
4

iex> LemonCore.UUID.version(LemonCore.UUID.uuid7())
7

Summary

Functions

Returns the 16 raw bytes of a canonical UUID string.

Generates a random (version 4) UUID.

Generates a time-ordered (version 7) UUID from the current time.

Generates a version 7 UUID stamped with unix_ms.

Returns the version digit of a canonical UUID string, or nil if it is not one.

Functions

decode(arg1)

@spec decode(String.t()) :: {:ok, <<_::128>>} | :error

Returns the 16 raw bytes of a canonical UUID string.

Examples

iex> LemonCore.UUID.decode("00000000-0000-4000-8000-000000000000")
{:ok, <<0, 0, 0, 0, 0, 0, 64, 0, 128, 0, 0, 0, 0, 0, 0, 0>>}

iex> LemonCore.UUID.decode("nope")
:error

uuid4()

@spec uuid4() :: String.t()

Generates a random (version 4) UUID.

uuid7()

@spec uuid7() :: String.t()

Generates a time-ordered (version 7) UUID from the current time.

uuid7(unix_ms)

@spec uuid7(integer()) :: String.t()

Generates a version 7 UUID stamped with unix_ms.

version(uuid)

@spec version(String.t()) :: pos_integer() | nil

Returns the version digit of a canonical UUID string, or nil if it is not one.

Examples

iex> LemonCore.UUID.version("00000000-0000-4000-8000-000000000000")
4

iex> LemonCore.UUID.version("not-a-uuid")
nil