nonpop/elm-purl/2.2.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, 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"

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"
maybeS : Maybe String -> Part a

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"
int : (a -> Int) -> Part a

A parameterized (variable) integer part.

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

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"
string : (a -> String) -> Part a

A parameterized string part.

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

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"
bool : (a -> Bool) -> Part a

A parameterized boolean part.

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

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 == "/"
hash : Part a

A hash-only part.

root
    |> append (s "base")
    |> append hash
    |> append (s "page")
    |> Url.toString {}
    --> "/base/#/page"
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"
maybeCustom : (a -> Maybe String) -> Part a

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

Infix operator to inject a hash.

root </> s "base" <#> s "page" </> int .page
    |> Url.toString { page = 42 }
--> "/base/#/page/42"
(<?>) : 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").