|elm-version||0.18.0 <= v < 0.19.0|
|Committed At||2017-08-07 22:22:03 UTC|
This library helps you turn URLs into nicely structured data.
This is a fork of the excellent evancz/url-parser library, with the dependency on
elm-lang/navigation removed. This avoids pulling in browser specific stuff like
dom, 'html', 'navigation', and 'virtual-dom'. It is meant for use with non-browser based applications, such as ktonon/elm-serverless, but it can still be used with SPAs.
There are two differences from
elm-lang/navigationfrom the dependencies
parseHashstill work with
Navigation.Locationbecause a type equivalent alias is defined for
parseString : Parser (a -> a) a -> String -> Maybe a
Here is a simplified REPL session showing a parser in action:
> import UrlParser exposing ((</>), s, int, string, parse) > parseString (s "blog" </> int) "#blog/42" Just 42 > parseString (s "blog" </> int) "#/blog/13" Just 13 > parseString (s "blog" </> int) "#/blog/hello" Nothing > parseString (s "search" </> string) "#search/dogs" Just "dogs" > parseString (s "search" </> string) "#/search/13" Just "13" > parseString (s "search" </> string) "#/search" Nothing
Normally you have to put many of these parsers to handle all possible pages though! The following parser works on URLs like
import UrlParser exposing (Parser, (</>), s, int, string, map, oneOf, parse) type Route = Blog Int | Search String route : Parser (Route -> a) a route = oneOf [ map Blog (s "blog" </> int) , map Search (s "search" </> string) ] -- parseString route "#/blog/58" == Just (Blog 58) -- parseString route "#/search/cat" == Just (Search "cat") -- parseString route "#/search/31" == Just (Search "31") -- parseString route "#/blog/cat" == Nothing -- parseString route "#/blog" == Nothing
Notice that we are turning URLs into nice union types, so we can use
case expressions to work with them in a nice way.
npm install npm test
I first saw this general idea in Chris Done’s formatting library. Based on that, Noah and I outlined the API you see in this library. Noah then found Rudi Grinberg’s post about type safe routing in OCaml. It was exactly what we were going for. We had even used the names
(</>)in our draft API! In the end, we ended up using the “final encoding” of the EDSL that had been left as an exercise for the reader. Very fun to work through!