Nanoid (Nanoid v3.0.0-rc.1)

Copy Markdown View Source

Elixir port of NanoID (https://github.com/ai/nanoid), a tiny, secure URL-friendly unique string ID generator.

Safe. It uses cryptographically strong random APIs and guarantees a proper distribution of symbols.

Compact. It uses a larger alphabet than UUID (A-Za-z0-9_-) and has a similar number of unique IDs in just 21 symbols instead of 36.

Quick start

iex> Nanoid.generate()
"mJUHrGXZBZpNX50x2xkzf"

iex> Nanoid.generate_with(size: 16, alphabet: "abcdef123")
"d1dcd2dee333cae1b"

Use generate_non_secure/0 or generate_non_secure_with/1 if cryptographic strength is not required.

Summary

Functions

Generates a secure NanoID using the default size and alphabet.

generate(size) deprecated

Generates a secure NanoID with the given size.

Generates a secure NanoID with the given size and alphabet.

Generates a non-secure NanoID using the default size and alphabet.

Generates a non-secure NanoID with the given size.

Generates a non-secure NanoID with the given size and alphabet.

Generates a non-secure NanoID using a keyword list of options.

Generates a secure NanoID using a keyword list of options.

Functions

generate()

@spec generate() :: binary()

Generates a secure NanoID using the default size and alphabet.

Quick-access shortcut, equivalent to generate_with([]).

Example

iex> Nanoid.generate()
"mJUHrGXZBZpNX50x2xkzf"

generate(size)

This function is deprecated. Use Nanoid.generate_with/1 instead.
@spec generate(non_neg_integer()) :: binary()

Generates a secure NanoID with the given size.

Deprecated — use generate_with(size: size).

generate(size, alphabet)

This function is deprecated. Use Nanoid.generate_with/1 instead.
@spec generate(non_neg_integer(), binary()) :: binary()

Generates a secure NanoID with the given size and alphabet.

Deprecated — use generate_with(size: size, alphabet: alphabet).

generate_non_secure()

@spec generate_non_secure() :: binary()

Generates a non-secure NanoID using the default size and alphabet.

Quick-access shortcut, equivalent to generate_non_secure_with([]).

Example

iex> Nanoid.generate_non_secure()
"mJUHrGXZBZpNX50x2xkzf"

generate_non_secure(size)

This function is deprecated. Use Nanoid.generate_non_secure_with/1 instead.
@spec generate_non_secure(non_neg_integer()) :: binary()

Generates a non-secure NanoID with the given size.

Deprecated — use generate_non_secure_with(size: size).

generate_non_secure(size, alphabet)

This function is deprecated. Use Nanoid.generate_non_secure_with/1 instead.
@spec generate_non_secure(non_neg_integer(), binary() | list()) :: binary()

Generates a non-secure NanoID with the given size and alphabet.

Deprecated — use generate_non_secure_with(size: size, alphabet: alphabet).

generate_non_secure_with(opts)

@spec generate_non_secure_with(keyword()) :: binary()

Generates a non-secure NanoID using a keyword list of options.

Always pass at least one option — for the no-options shortcut, use generate_non_secure/0.

Options

Examples

iex> Nanoid.generate_non_secure_with(size: 16)
"IRFa-VaY2b-NU5xX"

iex> Nanoid.generate_non_secure_with(alphabet: "abcdef123")
"d1dcd2dee333cae1bfdea"

iex> Nanoid.generate_non_secure_with(size: 12, alphabet: "abcdef123")
"d1dcd2dee333"

generate_with(opts)

@spec generate_with(keyword()) :: binary()

Generates a secure NanoID using a keyword list of options.

Always pass at least one option — for the no-options shortcut, use generate/0.

Options

Examples

iex> Nanoid.generate_with(size: 16)
"IRFa-VaY2b-NU5xX"

iex> Nanoid.generate_with(alphabet: "abcdef123")
"d1dcd2dee333cae1bfdea"

iex> Nanoid.generate_with(size: 12, alphabet: "abcdef123")
"d1dcd2dee333"