RomanErnst / erl / Erl

Library for parsing and constructing URLs

Types


type alias Url =
{ protocol : String
, username : String
, password : String
, host : List String
, port_ : Basics.Int
, path : List String
, hasLeadingSlash : Basics.Bool
, hasTrailingSlash : Basics.Bool
, hash : String
, query : Query 
}

Record that holds url attributes


type alias Query =
List ( String
, String 
}

List of tuples that holds the keys and values in the query string

Parse

parse : String -> Url

Parse a url string, returns an Erl.Url record

Erl.parse "http://api.example.com/users/1#x/1?a=1" == Erl.Url{...}

Parse helpers

extractHash : String -> String

Extract the hash (hash) from the url

extractHost : String -> String

Extract the host from the url

extractPath : String -> String

Extract the path from the url

extractProtocol : String -> String

Extract the protocol from the url

extractPort : String -> Basics.Int

Extract the port from the url

If no port is included in the url then Erl will attempt to add a default port:

Http -> 80 Https -> 443 FTP -> 21 SFTP -> 22

extractQuery : String -> String

Extract the query string from the url

Construct

new : Url

Generate an empty Erl.Url record

Erl.new ==

{ protocol = ""
, username = ""
, password = ""
, host = []
, path = []
, hasLeadingSlash = False
, hasTrailingSlash = False
, port_ = 0
, hash = ""
, query = Dict.empty
}

Mutation helpers

addQuery : String -> String -> Url -> Url

Adds key/value in query string

Erl.addQuery key value url

This doesn't replace existing keys, so if this is a duplicated this key is just added.

setQuery : String -> String -> Url -> Url

Set key/value in query string, removes any existing one if necessary.

Erl.setQuery key value url

removeQuery : String -> Url -> Url

Removes key from query string

Erl.removeQuery key url

clearQuery : Url -> Url

Clears the current query string

Erl.clearQuery url

appendPathSegments : List String -> Url -> Url

Append some path segments to a url

Erl.appendPathSegments ["hello", "world"] url

Serialize

toString : Url -> String

Generate url string from an Erl.Url record

url = { protocol = "http",
      , username = "",
      , password = "",
      , host = ["www", "foo", "com"],
      , path = ["users", "1"],
      , hasLeadingSlash = False
      , hasTrailingSlash = False
      , port_ = 2000,
      , hash = "a/b",
      , query = Dict.empty |> Dict.insert "q" "1" |> Dict.insert "k" "2"
      }

Erl.toString url == "http://www.foo.com:2000/users/1?k=2&q=1#a/b"

absoluteUrl : Url -> String

Generate absolute url string from an Erl.Url record

url = { protocol = "http",
      , username = "",
      , password = "",
      , host = ["www", "foo", "com"],
      , path = ["users", "1"],
      , hasLeadingSlash = False
      , hasTrailingSlash = False
      , port_ = 2000,
      , hash = "a/b",
      , query = Dict.empty |> Dict.insert "q" "1" |> Dict.insert "k" "2"
      }

Erl.toString url == "/users/1?k=2&q=1#a/b"

Serialization helpers

queryToString : Url -> String

Convert to a string only the query component of an url, this includes '?'

Erl.queryToString url == "?a=1&b=2"

Other helpers

getQueryValuesForKey : String -> Url -> List String

Gets values for a key in the query

url = Erl.parse "?a=1&b=2&a=3"

Erl.getQueryValuesForKey "a" url

== ["1", "3"]