This is an alternative site for discovering Elm packages. You may be looking for the official Elm package site instead.
A route parsing library
version 4.0.0
license BSD3
native-modules False
elm-version 0.18.0 <= v < 0.19.0
Tag 4.0.0
Committed At 2016-10-21 07:59:18 UTC
elm-lang/http 1.0.0 <= v < 2.0.0 1.0.0
elm-lang/core 5.0.0 <= v < 6.0.0 5.1.1
Bogdanp/elm-combine 3.0.0 <= v < 4.0.0 3.1.1

Modules

README

elm-route Build Status

elm package install Bogdanp/elm-route

This library defines functions for constructing route parsers. See the documentation of the Route module for more information. A full example is available at examples/app.

Usage

First define your routes:

module App.Routes exposing ( Route(..), match, route )

import Route exposing (..)

type Route
  = Home
  | About
  | Users
  | User Int
  | UserPosts Int
  | UserPost Int String
  | NotFound

home = Home := static ""
about = About := static "about"
users = Users := static "users"
user = User := static "users" </> int
userPosts = UserPosts := static "users" </> int </> static "posts"
userPost = UserPost := static "users" </> int </> string
routes = router [home, about, users, user, userPosts, userPost]

match : String -> Maybe Route
match = Route.match routes

toString : Route -> String
toString route =
  case route of
    Home -> reverse home []
    About -> reverse about []
    Users -> reverse users []
    User id -> reverse user [toString id]
    UserPosts id -> reverse userPosts [toString id]
    UserPost uid pid -> reverse userPost [toString uid, pid]
    NotFound -> Debug.crash "cannot route to NotFound"

You may then use them to match routes:

> import App.Routes as Routes exposing (Route(..), match)

> match "/"
Just Home : Maybe Route

> match "/users"
Just Users  : Maybe Route

> match "/i-dont-exist"
Nothing : Maybe Route

> match "/users/a"
Nothing : Maybe Route

> match "/users/1"
Just (User 1) : Maybe Route

> match "/users/1/hello-world"
Just (UserPost 1 "hello-world") : Maybe Route

And to convert routes to strings:

> import App.Routes as Routes

> Routes.toString Home
"/" : String

> Routes.toString About
"/about" : String

> Routes.toString (UserPost 1 "hello")
"/users/1/hello" : String

To use it with Navigation, define match in terms of Location

match : Location -> Route
match location =
  location.pathname
    |> Routes.match routes
    |> Maybe.withDefault NotFound

then use it in your Program:

import App.Routes as Routes
import Navigation exposing (Location)

type Msg
  = ChangeRoute Sitemap

parseRoute : Location -> Msg
parseRoute =
  Routes.match >> ChangeRoute

main : Program Never Model Msg
main =
    Navigation.program parseRoute
        { init = init
        , update = update
        , view = view
        , subscriptions = subscriptions
        }

See the examples directory and tests/Tests.elm for more.