Skip to content

Update URLSearchParams #101

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

Merged
merged 1 commit into from
May 13, 2025
Merged
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
28 changes: 28 additions & 0 deletions src/FetchAPI/URLSearchParams.res
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ Deletes the given search parameter, and its associated value, from the list of a
@send
external delete: (urlSearchParams, ~name: string, ~value: string=?) => unit = "delete"

/**
Returns key/value pairs in the same order as they appear in the query string.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/URLSearchParams/entries)
*/
@send
external entries: urlSearchParams => Iterator.t<(string, string)> = "entries"

/**
Returns the first value associated to the given search parameter.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/URLSearchParams/get)
Expand All @@ -54,6 +61,13 @@ Returns a Boolean indicating if such a search parameter exists.
@send
external has: (urlSearchParams, ~name: string, ~value: string=?) => bool = "has"

/**
Returns an iterator allowing iteration through all keys contained in this object.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/URLSearchParams/keys)
*/
@send
external keys: urlSearchParams => Iterator.t<string> = "keys"

/**
Sets the value associated to a given search parameter to the given value. If there were several values, delete the others.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/URLSearchParams/set)
Expand All @@ -66,3 +80,17 @@ external set: (urlSearchParams, ~name: string, ~value: string) => unit = "set"
*/
@send
external sort: urlSearchParams => unit = "sort"

/**
Returns the query string suitable for use in a URL, without the question mark.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/URLSearchParams/toString)
*/
@send
external toString: urlSearchParams => string = "toString"

/**
Returns an iterator allowing iteration through all values contained in this object.
[Read more on MDN](https://developer.mozilla.org/docs/Web/API/URLSearchParams/values)
*/
@send
external values: urlSearchParams => Iterator.t<string> = "values"
30 changes: 30 additions & 0 deletions tests/FetchAPI/URLSearchParams__test.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions tests/FetchAPI/URLSearchParams__test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
let params1 = URLSearchParams.make3(~init="foo=1&bar=2")
params1->URLSearchParams.keys->Iterator.toArray->Array.forEach(Console.log)

let params2 = URLSearchParams.make3(~init="?foo=1&bar=b")
params2->URLSearchParams.values->Iterator.toArray->Array.forEach(Console.log)

let params3 = URLSearchParams.make3(~init="?foo=1&bar=b")
params3
->URLSearchParams.entries
->Iterator.toArray
->Array.forEach(((key, value)) => Console.log2(key, value))

let paramStr = params3->URLSearchParams.toString
Loading