elmish / browser

Routing and Navigation for browser apps
https://elmish.github.io/browser
Other
35 stars 20 forks source link

Add flagParam for valueless query string parameters #17

Closed toburger closed 4 years ago

toburger commented 5 years ago

Description

To deal with boolean paramaters it would be nice to have a 'flagParam' function, where the presence of the parameter is enough to set it to true, otherwise the value is false.

Sometimes it is enough to simply need the presence of a parameter.

Repro code

../mypage?nav

where

flagParam "nav" returns true if nav is present, otherwise false.

With the current customParam function I get None when I provide only the key part of the query string parameter so it is not possible to differentiate between the presence or absence of the parameter and implement flagParam based on customParam.

Related information

MangelMaxime commented 5 years ago

The solution could be to change:

let internal toKeyValuePair (segment:string) =
    match segment.Split('=') with
    | [| key; value |] ->
        Option.tuple (Option.ofFunc JS.decodeURI key) (Option.ofFunc JS.decodeURI value)
| _ -> None

to something like

let internal toKeyValuePair (segment:string) =
    match segment.Split('=') with
    | [| key; value |] ->
        Option.tuple (Option.ofFunc JS.decodeURI key) (Option.ofFunc JS.decodeURI value)
    | [| key |] ->
        Option.tuple (Option.ofFunc JS.decodeURI key) ""
    | _ -> None

Like that if we have Some "", we know that the parameters was presents in either the form ?nav= or ?nav

toburger commented 5 years ago

Changing it to Some "" would make it possible to define the flagParam. 👍

et1975 commented 5 years ago

Any idea what the w3c specs say about value of such a param?

MangelMaxime commented 5 years ago

Last time I checked, there isn't much specification for what is a valid query parameters format.

And in general, when working on a web server, they implements the specification as others do to keep compatibility.

The example, I saw at the time was how to define a list in an URL. They do it like that: ?field1=1&field1=2 which represents something likelet field1 = [1; 2].

et1975 commented 5 years ago

I'm not so much concerned with validity of the query, but with the value. As in, is it supposed to be undefined instead of "", for example. @toburger are you formatting this URL yourself or getting a redirect like that from somewhere?

toburger commented 5 years ago

@et1975 I have control over the URL, so I can use (and use ATM) ?nav=1. It's just not very convenient to have to specify a value to the parameter if it's only intent is to activate/deactivate a feature.

Concerning the validity of a valueless parameter I checked yesterday if it is valid, before posting this issue, and found the following StackOverflow thread: https://stackoverflow.com/questions/4557387/is-a-url-query-parameter-valid-if-it-has-no-value

et1975 commented 4 years ago

Released in the new package https://github.com/elmish/urlParser as Fable.Elmish.UrlParser@1.0.0-alpha-1 and it's now available for CLR as well (Elmish.UrlParser@1.0.0-alpha-1). elmish-urlParser will be a dependency for elmish-browser from v4 onward.