View Source Sqids behaviour (sqids v0.2.1)

Sqids API

ℹī¸ Check out the docs entry page on how to make Sqids easier to use by not passing the context on every encode/decode call, through either:

  • creation of context at compile time under a module attribute,
  • or the use Sqids macro to generate functions that retrieve context transparently.

Summary

Types

Wrapper type for Elixir 1.13 or older

Opts for new/1

t()

Context for Sqids

Callbacks

Returns Supervisor child spec for callback module.

Functions

Decodes an id into zero or more numbers according to sqids's alphabet.

Tries to encode zero or more numbers into as an id, according to sqids's alphabet, blocklist, and minimum length. Returns an error otherwise.

Encodes zero or more numbers into an id, according to sqids's alphabet, blocklist, and minimum length. Raises in case of error.

Creates a context used for both encoding and decoding.

Like new/0 and new/1 but raises in case of error.

Types

enumerable(t)

(since 0.1.0)
@type enumerable(t) :: Enumerable.t(t)

Wrapper type for Elixir 1.13 or older

opts()

(since 0.1.0)
@type opts() :: [
  alphabet: String.t(),
  min_length: non_neg_integer(),
  blocklist: enumerable(String.t())
]

Opts for new/1

t()

(since 0.1.0)
@opaque t()

Context for Sqids

Callbacks

child_spec()

(since 0.1.0)
@callback child_spec() :: Supervisor.child_spec()

Returns Supervisor child spec for callback module.

Functions

decode!(sqids, id)

(since 0.1.0)
@spec decode!(sqids, id) :: numbers
when sqids: t(), id: String.t(), numbers: [non_neg_integer()]

Decodes an id into zero or more numbers according to sqids's alphabet.

Like in the reference implementation, the presence of unknown characters within id will result in an empty list being returned.

encode(sqids, numbers)

(since 0.1.0)
@spec encode(sqids, numbers) :: {:ok, id} | {:error, term()}
when sqids: t(), numbers: enumerable(non_neg_integer()), id: String.t()

Tries to encode zero or more numbers into as an id, according to sqids's alphabet, blocklist, and minimum length. Returns an error otherwise.

encode!(sqids, numbers)

(since 0.1.0)
@spec encode!(sqids, numbers) :: id
when sqids: t(), numbers: enumerable(non_neg_integer()), id: String.t()

Encodes zero or more numbers into an id, according to sqids's alphabet, blocklist, and minimum length. Raises in case of error.

new(opts \\ [])

(since 0.1.0)
@spec new(opts()) :: {:ok, t()} | {:error, term()}

Creates a context used for both encoding and decoding.

Can receive a list of zero or more opts/0:

  • alphabet: a case and order -sensitive string containing the chars of which generated IDs will be made of;
  • min_length: the minimum length of your generated IDs (padding added if needed);
  • blocklist: an enumerable collection of strings which shouldn't appear in generated IDs.

Returns error if any of the opts/0 is invalid.

new!(opts \\ [])

(since 0.1.1)
@spec new!(opts()) :: t()

Like new/0 and new/1 but raises in case of error.