TheSpyder / rescript-webapi

ReScript bindings to the DOM and other Web APIs
http://tinymce.github.io/rescript-webapi/api/Webapi/
Other
149 stars 36 forks source link

Non-mutating URLSearchParams set? #105

Open AlexMoutonNoble opened 2 years ago

AlexMoutonNoble commented 2 years ago

Hi All Its given me some pain that this function doesnt return a URLSearchParams that can be passed back up the chain to location. How about a PR for a set that returns t?

Thanks Alex

TheSpyder commented 2 years ago

Can you please post some sample JavaScript that shows what you're trying to achieve? I think I know what you're asking for but I'd like to make sure.

We are going to break a lot of APIs with version 2, as we adopt the objects with optional fields feature of ReScript 10, so that might be an opportunity to simply add the t return to set directly.

AlexMoutonNoble commented 2 years ago

Trying to assert a query parameter from Location i ended up with:

            let dict = Window.location()
            ->Webapi.Dom.Location.search
            ->Webapi.Url.URLSearchParams.make
            ->Webapi.Url.URLSearchParams.entries
            ->Js.Array.from
            ->Js.Dict.fromArray

            dict->Js.Dict.set("output", query)

            dict
            ->Webapi.Url.URLSearchParams.makeWithDict
            ->Webapi.Url.URLSearchParams.toString
            ->Webapi.Dom.Location.setSearch(Window.location(), _)