IDUtils.Schemes.URL (id_utils v0.1.0)

Copy Markdown View Source

URL (Uniform Resource Locator) scheme.

For the purposes of this library, an identifier URL will be matched if it has a scheme and a host. While mailto:test@example.com is a perfectly valid URL, it is not considered a "valid" identifier URL here.

Parsing

Elixir's URI module implements RFC 3986, not the WHATWG URL standard followed by browsers, which in general tends to be more lenient around some edge cases (e.g., unencoded square brackets in query strings, backslashes used as path separators). These should generally not be relevant in an identifier context, but be aware this module might reject URLs that are accepted by browsers.

Summary

Functions

Builds a URI for the given URL string.

Checks whether a string is a URL.

Normalizes a URL.

Functions

build_uri(string)

Builds a URI for the given URL string.

Examples

iex> IDUtils.Schemes.URL.build_uri("https://gallica.bnf.fr/f29")
{:ok, %URI{scheme: "https", host: "gallica.bnf.fr", path: "/f29", port: 443}}

match?(string)

Checks whether a string is a URL.

Examples

iex> IDUtils.Schemes.URL.match?("https://gallica.bnf.fr/f29")
true

iex> IDUtils.Schemes.URL.match?("mailto:test@example.com")
false

normalize(string)

Normalizes a URL.

Examples

iex> IDUtils.Schemes.URL.normalize("https://example.com/12148")
{:ok, "https://example.com/12148"}

iex> IDUtils.Schemes.URL.normalize("https://example.com:443")
{:ok, "https://example.com/"}

iex> IDUtils.Schemes.URL.normalize("example.com/x6np1wh8k")
{:error, :invalid_identifier}