nonpop/elm-purl/2.0.2

NOTE: this package is not compatible with Elm 0.19.1

Url

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"

Types

type Url a

A URL parameterized over the type a, which is typically a record containing a field for each parameterized Part.

type Part a

A parameterized part (segment or query value) of a URL.

Builders

root : Url a

The root URL.

root |> Url.toString () --> "/"
append : Part a -> Url a -> Url a

Append a segment to the URL.

s : String -> Part a

An unparameterized (static) part.

root |> append (s "users") |> Url.toString () --> "/users"
int : (a -> Int) -> Part a

A parameterized (variable) integer part.

root
    |> append (s "users")
    |> append (int .id)
    |> Url.toString { id = 42 }
    --> "/users/42"
string : (a -> String) -> Part a

A parameterized string part.

root
    |> append (s "say")
    |> append (string .word)
    |> Url.toString { word = "Hello" }
    --> "/say/Hello"
bool : (a -> Bool) -> Part a

A parameterized boolean part.

root
    |> append (bool .show)
    |> Url.toString { show = True }
    --> "/true"
custom : (a -> String) -> Part a

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"
appendParam : String -> Part a -> Url a -> Url a

Append a query parameter to the URL.

root
    |> append (s "part")
    |> appendParam "show" (bool .show)
    |> Url.toString { show = True }
    --> "/part?show=true"
(</>) : Url a -> Part a -> Url a

Infix version of append.

(<?>) : Url a -> ( String, Part a ) -> Url a

Infix version of appendParam

Presenting

toString : a -> Url a -> String

Give a string representation of the URL, given a value for the parameter.

(@) : Url a -> a -> String

Infix version of Url.toString ("evaluate at").