Normalize a url. This is useful for displaying, storing, sorting, etc.
Add :normalize_url to your list of dependencies in mix.exs
def deps do
[{:normalize_url, "~> 0.0.2"}]
end
Ensure :normalize_url is started before your application
def application do
[applications: [:normalize_url]]
end
NormalizeUrl.normalize_url("https://www.google.com?b=b&a=a")
# => "https://google.com?a=a&b=b"
NormalizeUrl.normalize_url("//foo.bar#about")
# => "http://foo.bar"
Type: boolean
Default: true
Remove www.
from the url
NormalizeUrl.normalize_url("http://www.johnotander.com")
# => "http://johnotander.com"
NormalizeUrl.normalize_url("http://www.johnotander.com", [strip_www: false])
# => "http://www.johnotander.com"
Type: boolean
Default: true
Remove #framents
from the url
NormalizeUrl.normalize_url("http://johnotander.com#about.html")
# => "http://johnotander.com"
NormalizeUrl.normalize_url("http://www.johnotander.com", [strip_fragment: false])
# => "http://johnotander.com#about.html"
Type: boolean
Default: true
Normalize relative protocols
NormalizeUrl.normalize_url("//johnotander.com#about")
# => "http://johnotander.com"
NormalizeUrl.normalize_url("//www.johnotander.com", [normalize_protocol: false])
# => "//johnotander.com"
Type: boolean
Default: false
Adds a trailing slash for the root path, if the path is empty.
NormalizeUrl.normalize_url("http://example.com")
# => "http://example.com"
NormalizeUrl.normalize_url("http://example.com", [add_root_path: true])
# => "http://example.com/"
mix test
- Elixir port from the
normalize-url
node module by Sindre Sorhus.
MIT
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
Crafted with <3 by John Otander (@4lpine).