DarkMatter.ShortIds (DarkMatter v1.1.4) View Source

Generates a unique short_id for use in abreviated non-uuid id aliases.

The alphabet is truncated based on the reasoning found here: https://fiznool.com/blog/2014/11/16/short-id-generation-in-javascript/

Link to this section Summary

Functions

Handles parsing the response and raising an error in the event of failure

Link to this section Types

Specs

cipher() :: String.t()

Specs

key() :: pos_integer()

Specs

keys() :: [pos_integer()]

Specs

options() :: [alphabet: binary(), salt: binary(), min_len: pos_integer()]

Link to this section Functions

Link to this function

decode(cipher, opts \\ [alphabet: "ABDEGJKMNPQRVWXYZ23456789", min_len: 3])

View Source

Specs

decode(cipher(), options()) :: {:ok, keys()} | :error

Decodes a given key from a unique short-id

## Examples

iex> decode("NY6")
{:ok, [5]}
Link to this function

decode_singular_cipher(cipher, opts \\ [alphabet: "ABDEGJKMNPQRVWXYZ23456789", min_len: 3])

View Source

Specs

decode_singular_cipher(cipher(), options()) :: {:ok, key()} | :error

Handles parsing the response

## Examples

iex> decode_singular_cipher("NY6")
{:ok, 5}

iex> decode_singular_cipher("")
:error

iex> decode_singular_cipher("RZJRXKW936")
:error
Link to this function

decode_singular_cipher!(cipher)

View Source

Specs

decode_singular_cipher!(cipher()) :: key()

Handles parsing the response and raising an error in the event of failure

## Examples

iex> decode_singular_cipher!("NY6")
5

iex> decode_singular_cipher!("")
** (ArgumentError) ShortIds: invalid cipher: ""
Link to this function

encode(key, opts \\ [alphabet: "ABDEGJKMNPQRVWXYZ23456789", min_len: 3])

View Source

Specs

encode(key() | keys(), options()) :: cipher()

Encodes a given cipher to a unique short-id

## Examples

iex> encode(5)
"NY6"

iex> encode([5, 38, 12345])
"RZJRXKW936"