A simpler alternative to the elm/url
package.
Url.Codec
allows you to define both the URL parser and the URL builder at the same time.Url.SimpleParser
only deals with parsing, but has a nicer API thanelm/url
.
Accepts both the Url
type and strings like:
post/hello-world/comments/2
post/hello-world/comments/2/
/post/hello-world/comments/2
/post/hello-world/comments/2/
/path?admin=true
/path?admin=true&fireZeMissiles=1
/path?highlight=123&highlight=154
/path?foo&bar
/path#something
import Url.Codec exposing (Codec)
type Route
= Topic String
| Blog Int String
topicCodec : Codec Route
topicCodec =
Url.Codec.succeed Topic isTopicRoute
|> Url.Codec.s "topic"
|> Url.Codec.string getTopicSlug
-- needed for Url.Codec
-- not needed for Url.SimpleParser
getTopicSlug : Route -> Maybe String
getTopicSlug route =
case route of
Topic slug ->
Just slug
_ ->
Nothing
-- needed for Url.Codec
-- not needed for Url.SimpleParser
isTopicRoute : Route -> Bool
isTopicRoute route =
case route of
Topic _ ->
True
_ ->
False
Url.Codec.parsePath [topicCodec] "topic/hello-world"
--> Ok (Topic "hello-world")
Url.Codec.toString [topicCodec] (Topic "foo-bar-baz")
--> Just "topic/foo-bar-baz"
For a more fleshed-out example see example/CodecExample.elm and example/SimpleParserExample.elm.