Norma (Norma v2.0.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_root_path
  • add_trailing_slash
  • remove_www
  • remove_scheme
  • restore_old_query_behavior — rebuild the query the 1.x way (keys sorted, duplicate keys collapsed to the last value) instead of preserving input order and duplicates

Host case is always folded to lowercase via RFC 3986 normalization (:uri_string.normalize/1). The 1.x downcase_host option was removed in 2.0.

Check the README for the full examples.

Examples

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