sporto/erl/7.0.0

NOTE: this package is not compatible with Elm 0.19.1

Erl

Library for parsing and constructing URLs

Types

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

Record that holds url attributes

type alias Query =
Dict String String

A Dict that holds keys and values for 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 -> Int

Extract the port from the url

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 = [],
  port' = 0,
  hash = [],
  query = Dict.empty
}
toString : Url -> String

Generate url string from an Erl.Url record

url = {
  protocol = "http",
  username = "",
  password = "",
  host = ["www", "foo", "com"],
  path = ["users", "1"],
  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#a/b?k=2&q=1"

Mutation helpers

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

Set key/value in query string

Erl.addQuery key value url
setQuery : String -> String -> Url -> Url

Set key/value in query string, removes any existing ones

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