Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented convenience functions for Float query parameters. #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/Url/Builder.elm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Url.Builder exposing
( absolute, relative, crossOrigin, custom, Root(..)
, QueryParameter, string, int, toQuery
, QueryParameter, string, int, float, toQuery
)


Expand Down Expand Up @@ -194,6 +194,20 @@ int key value =
QueryParameter (Url.percentEncode key) (String.fromInt value)


{-| Create a percent-encoded query parameter.

absolute ["search"] [ float "lat" 48.4428425, float "lng" 1.4812848 ]
-- "/search?lat=48.4428425&lng=1.4812848"

Writing `float key n` is the same as writing `string key (String.fromFloat n)`.
So this is just a convenience function, making your code a bit shorter!

-}
float : String -> Float -> QueryParameter
float key value =
QueryParameter (Url.percentEncode key) (String.fromFloat value)


{-| Convert a list of query parameters to a percent-encoded query. This
function is used by `absolute`, `relative`, etc.

Expand Down
30 changes: 29 additions & 1 deletion src/Url/Parser/Query.elm
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Url.Parser.Query exposing
( Parser, string, int, enum, custom
( Parser, string, int, float, enum, custom
, map, map2, map3, map4, map5, map6, map7, map8
)

Expand Down Expand Up @@ -96,6 +96,34 @@ int key =
Nothing


{-| Handle `Float` parameters.


height : Parser (Maybe Float)
height =
float "height"

-- ?height=2 == Just 2
-- ?height=17.876 == Just 17.876
-- ?height=two == Nothing
-- ?sort=date == Nothing
-- ?height=1.2&height=3.19 == Nothing

Check out [`custom`](#custom) if you need to handle multiple `height` parameters.

-}
float : String -> Parser (Maybe Float)
float key =
custom key <|
\stringList ->
case stringList of
[ str ] ->
String.toFloat str

_ ->
Nothing


{-| Handle enumerated parameters. Maybe you want a true-or-false parameter:

import Dict
Expand Down