Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

mazingstudio/norma

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

36 Commits

Repository files navigation

Norma

Hex.pm Hex.pm Hex.pm Maintenance GitHub issues Travis

Normalize URLs to the format you need.

Installation

Add Norma to your list of dependencies in mix.exs:

def deps do
 [
 # get always the latest Γ±___Γ±
 {:norma, ">= 0.0.0"}
 ]
end

Note on compatibility

Norma depends heavily on the implementation of the URI module (part of the standard library), which was recently changed when adding support for Erlang 20. Use the module history as a guide if you encounter any weird error (specially if you suspect it's related to a regex).

Usage

iex> Norma.normalize("mazing.studio")
> "http://mazing.studio"
iex> Norma.normalize_if_valid("mazing.studio")
> {:ok, "http://mazing.studio"}
iex> Norma.normalize_if_valid("mazing")
> {:error, "Not an URL."}
iex> options = %{remove_fragment: true, force_root_path: true, remove_www: true}
iex> Norma.normalize("//www.mazing.studio:1337/test#test", options)
> "http://mazing.studio/"

Options

  • Remove scheme:

    iex> Norma.normalize("https://mazing.studio", %{remove_scheme: true})
    > "mazing.studio"
  • Remove fragment:

    iex> Norma.normalize("https://mazing.studio#test", %{remove_fragment: true})
    > "https://mazing.studio"
  • Add trailing slash:

    iex> Norma.normalize("https://mazing.studio/test", %{add_trailing_slash: true})
    > "https://mazing.studio/test/"
  • Force root path:

    iex> Norma.normalize("https://mazing.studio/test", %{force_root_path: true})
    > "https://mazing.studio/"
  • Remove www:

    iex> Norma.normalize("https://www.mazing.studio", %{remove_www: true})
    > "https://mazing.studio"
  • Downcase host:

    iex> Norma.normalize("https://EXAMPLE.COM/FAQS", %{downcase_host: true})
    > "https://example.com/FAQS"

With Ecto

def creation_changeset(params) do
 norma_options = %{
 remove_www: true,
 force_root_path: true,
 remove_fragment: true
 }
 %MyEntity{}
 |> cast(params, @fields)
 |> put_change(:url, Norma.normalize(params.url, norma_options))
end

Contributing

Adding options

  1. Add support for the option in /lib/norma/normalizer.ex. Prefer pattern matching and guards over ifs and cases.
  2. Add a test in /test/norma_test.exs.
  3. Add documentation to the README.
  4. Send a PR πŸŽ‰

Maintainers


A Mazing project

Mazing Studio

Sponsored by Mazing Studio

We love to solve problems using Elixir and Go.

Have a project you need help with? Tell us about it!

AltStyle γ«γ‚ˆγ£γ¦ε€‰ζ›γ•γ‚ŒγŸγƒšγƒΌγ‚Έ (->γ‚ͺγƒͺγ‚ΈγƒŠγƒ«) /