A tiny library for building parameterized URLs. It is intended to be used with records to give the parameters names and therefore reducing errors.
userUrl : Url { id : Int, show : Bool }
userUrl = root </> s "users" </> int .id <?> ("show", bool .show)
userUrl @ { id = 42, show = True } --> "/users/42?show=true"
userUrlWithoutInfix : Url { id : Int, show : Bool }
userUrlWithoutInfix = root
|> append (s "users")
|> append (int .id)
|> appendParam "show" (bool .show)
userUrlWithoutInfix
|> Url.toString { id = 42, show = True } --> "/users/42?show=true"A parameterized part (segment or query value) of a URL.
The root URL.
root |> Url.toString () --> "/"Append a segment to the URL.
An unparameterized (static) part.
root |> append (s "users") |> Url.toString () --> "/users"An unparameterized (static) part with a Maybe value; it is omitted when the value is Nothing.
root
|> append (maybeS (Just "users"))
|> append (maybeS Nothing)
|> append (maybeS (Just "1"))
|> Url.toString () == "/users/1"A parameterized (variable) integer part.
root
|> append (s "users")
|> append (int .id)
|> Url.toString { id = 42 }
--> "/users/42"A parameterized (variable) integer part with a Maybe value; it is omitted when the value is Nothing.
url =
root
|> append (s "users")
|> append (maybeInt .id)
|> append (s "images")
Url.toString { id = Just 42 } url == "/users/42/images"
Url.toString { id = Nothing } url == "/users/images"A parameterized string part.
root
|> append (s "say")
|> append (string .word)
|> Url.toString { word = "Hello" }
--> "/say/Hello"A parameterized string part with a Maybe value; it is omitted when the value is Nothing.
url =
root
|> append (s "say")
|> append (maybeString .word)
|> append (s "world")
Url.toString { word = Just "Hello" } url == "/say/Hello/world"
Url.toString { word = Nothing } url == "/say/world"A parameterized boolean part.
root
|> append (bool .show)
|> Url.toString { show = True }
--> "/true"A parameterized boolean part with a Maybe value; it is omitted when the value is Nothing.
url =
root
|> append (maybeBool .show)
Url.toString { show = Just True } url == "/true"
Url.toString { show = Nothing } url == "/"A hash-only part.
root
|> append (s "base")
|> append hash
|> append (s "page")
|> Url.toString {}
--> "/base/#/page"Build a custom part.
import String.Extra exposing (fromInt)
root
|> append (custom (.ids >> List.map fromInt >> String.join ";"))
|> Url.toString { ids = [1, 2, 3] }
--> "/1%3B2%3B3"Build a custom part with a Maybe value; it is omitted when the value is Nothing.
import String.Extra exposing (fromInt)
root
|> append (custom (.ids >> List.map fromInt >> String.join ";" >> Just))
|> Url.toString { ids = [1, 2, 3] }
== "/1%3B2%3B3"Append a query parameter to the URL.
root
|> append (s "part")
|> appendParam "show" (bool .show)
|> Url.toString { show = True }
--> "/part?show=true"Infix version of append.
Infix operator to inject a hash.
root </> s "base" <#> s "page" </> int .page
|> Url.toString { page = 42 }
--> "/base/#/page/42"Infix version of appendParam
Give a string representation of the URL, given a value for the parameter.
Infix version of Url.toString ("evaluate at").
A URL parameterized over the type
a, which is typically a record containing a field for each parameterizedPart.