nonpop/elm-purl/1.0.0

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 }
userUrl = root </> s "users" </> int .id


userUrl @ { id = 42 } == "/users/42"

Types

type Url a

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

type Segment a

A parameteried segment of a URL.

Builders

root : Url a

The root URL.

root |> toString () == "/"
append : Segment a -> Url a -> Url a

Append a segment to the URL.

s : String -> Segment a

An unparameterized (static) segment.

root |> append (s "users") |> toString () == "/users"
int : (a -> Int) -> Segment a

A parameterized (variable) integer segment.

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

A parameterized string segment.

root
    |> append (s "say")
    |> append (string .word)
    |> toString { word = "Hello" }
    == "/say/Hello"
custom : (a -> String) -> Segment a

Build a custom segment.

root
    |> append (custom (.ids >> List.map fromInt >> String.join ";"))
    |> toString { ids = [1, 2, 3] }
    == "/1;2;3"
(</>) : Url a -> Segment a -> Url a

Infix version of append.

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 toString ("evaluate at").