Norma (Norma v1.9.0)

Copy Markdown View Source

URL normalization.

Norma parses a URL string and rewrites it into a canonical form, defaulting the scheme to http and applying the transformations requested through the options map.

Two entry points are provided:

See normalize_if_valid/2 for the list of supported options.

Summary

Functions

Similar to normalize_if_valid/2, but will return the given string unchanged if it's not an URL.

Normalize URL according to the options given. Defaults scheme to http.

Functions

normalize(url, options \\ %{})

@spec normalize(String.t(), map()) :: String.t()

Similar to normalize_if_valid/2, but will return the given string unchanged if it's not an URL.

Examples

iex> Norma.normalize!("//www.mazing.studio", %{remove_www: true})
"http://mazing.studio"

normalize_if_valid(url, options \\ %{})

@spec normalize_if_valid(String.t(), map()) ::
  {:ok, String.t()} | {:error, String.t()}

Normalize URL according to the options given. Defaults scheme to http.

Options

  • remove_fragment
  • force_root_path
  • add_trailing_slash
  • remove_www
  • downcase_host

Check the README for the full examples.

Examples

iex> Norma.normalize("//www.mazing.studio", %{remove_www: true})
{:ok, "http://mazing.studio"}