Path

Represents the path portion of a URL (not query parameters, fragment, protocol, port, etc.).

This helper lets you combine together path parts without worrying about having too many or too few slashes. These two examples will result in the same URL, even though the first example has trailing and leading slashes, and the second does not.

Path.join [ "/blog/", "/post-1/" ]
    |> Path.toAbsolute
--> "/blog/post-1"

Path.join [ "blog", "post-1" ]
    |> Path.toAbsolute
--> "/blog/post-1"

We can also safely join Strings that include multiple path parts, a single path part per string, or a mix of the two:

Path.join [ "/articles/archive/", "1977", "06", "10", "post-1" ]
    |> Path.toAbsolute
--> "/articles/archive/1977/06/10/post-1"

Creating Paths

type Path

The path portion of the URL, normalized to ensure that path segments are joined with /s in the right places (no doubled up or missing slashes).

join : List String -> Path

Create a Path from multiple path parts. Each part can either be a single path segment, like blog, or a multi-part path part, like blog/post-1.

fromString : String -> Path

Create a Path from a path String.

Path.fromString "blog/post-1/"
    |> Path.toAbsolute
    |> Expect.equal "/blog/post-1"

Turning Paths to String

toAbsolute : Path -> String

Turn a Path to an absolute URL (with no trailing slash).

toRelative : Path -> String

Turn a Path to a relative URL.

toSegments : Path -> List String