Namor (Namor v1.0.4)

View Source

A subdomain-safe name generator. Check out the README to get started.

Summary

Functions

Generates a random name from a Namor dictionary.

Generates a random name from a custom dictionary.

Get Namor's dictionaries.

Get Namor's reserved word list.

Checks whether a value exists in Namor's list of reserved words.

Checks whether a value exists in a provided list of reserved words.

Checks whether a given value is a valid subdomain.

Types

dictionary_type()

@type dictionary_type() :: :default | :rugged

salt_type()

@type salt_type() :: :numbers | :letters | :mixed

Functions

generate(opts \\ [])

(macro)
@spec generate(
  words: integer(),
  salt: integer(),
  salt_type: salt_type(),
  separator: binary(),
  dictionary: dictionary_type()
) :: {:ok, binary()} | {:error, atom()}

Generates a random name from a Namor dictionary.

Returns {:ok, name} for a successfully generated name, or {:error, reason} if something goes wrong. See Custom Dictionaries for instructions on how to use a custom dictionary.

Options

  • :words - Number of words to generate. Must be <=4. Defaults to 2.
  • :salt - Length of the salt to append. Must be >=0. Defaults to 0.
  • :salt_type - Whether the salt should contain numbers, letters, or both. Defaults to :mixed.
  • :separator - String to use as a separator between words. Defaults to -.
  • :dictionary - Namor dictionary to use. Defaults to :default.

Error Reasons

  • :dict_not_found - Could not find the specified dictionary.

Example

iex> require Namor
Namor

iex> Namor.generate(words: 3, dictionary: :rugged)
{:ok, "savage-whiskey-stain"}

generate(opts, dict)

@spec generate(
  [
    words: integer(),
    salt: integer(),
    salt_type: salt_type(),
    separator: binary()
  ],
  Namor.Dictionary.t()
) :: {:ok, binary()}

Generates a random name from a custom dictionary.

Returns {:ok, name} for a successfully generated name. Takes the same options as generate/1 with the exception of :dictionary.

get_dictionaries()

@spec get_dictionaries() :: %{required(binary()) => Namor.Dictionary.t()}

Get Namor's dictionaries.

get_reserved()

@spec get_reserved() :: [binary()]

Get Namor's reserved word list.

reserved?(value)

(macro)
@spec reserved?(binary()) :: boolean()

Checks whether a value exists in Namor's list of reserved words.

See reserved?/2 for more info. See Custom Dictionaries for instructions on how to use a custom reserved word list.

reserved?(value, reserved)

@spec reserved?(binary(), [binary()]) :: boolean()

Checks whether a value exists in a provided list of reserved words.

Before checking, the value is stripped of all special characters. So for example, log-in will still return true if ["login"] is passed to reserved.

subdomain?(value)

@spec subdomain?(binary()) :: boolean()

Checks whether a given value is a valid subdomain.

Valid subdomains contain no special characters other than -, and are 63 characters or less.

with_salt(value, length, separator \\ "-", type \\ :mixed)

@spec with_salt(binary(), integer(), binary(), salt_type()) :: binary()

Appends a salt to a value.

If you want to use a custom charlist for the salt, use Namor.Helpers.get_salt/2 instead.