Last active
May 29, 2023 22:59
-
-
Save mossprescott/70ad600f3a69935d30fe093fd034da54 to your computer and use it in GitHub Desktop.
Parse a single query param
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Url | |
import Url.Parser | |
import URL.Parser.Query | |
parseQuizNumber : String -> Maybe Int | |
parseQuizNumber urlString = | |
let | |
dropPath : Url -> Url | |
dropPath url = | |
{ url | path = "" } | |
parseUrl : Url -> Maybe (Maybe Int) | |
parseUrl = Url.Parser.parse (Url.Parser.query (Url.Parser.Query.int "quiz")) | |
in | |
case Url.fromString urlStr |> Maybe.andThen (dropPath >> parseUrl) of | |
Just (Just qid) -> | |
-- The URL was valid, the parameter was present and contained an int: | |
Just qid | |
Just Nothing -> | |
-- The URL was valid, we parsed it, but there was no "quiz" param, or it wasn't an int | |
Nothing | |
Nothing -> | |
-- The URL was invalid | |
Nothing | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The next step to use is to modify your
main
so it looks something like this:And, in index.html: