From 22f07dfb490a601145ecfe93f149c0af4c1fe4de Mon Sep 17 00:00:00 2001 From: Stephanos Komnenos Date: Tue, 13 May 2025 15:22:59 +0000 Subject: [PATCH] Update `URLSearchParams` --- src/FetchAPI/URLSearchParams.res | 28 ++++++++++++++++++++++ tests/FetchAPI/URLSearchParams__test.js | 30 ++++++++++++++++++++++++ tests/FetchAPI/URLSearchParams__test.res | 13 ++++++++++ 3 files changed, 71 insertions(+) create mode 100644 tests/FetchAPI/URLSearchParams__test.js create mode 100644 tests/FetchAPI/URLSearchParams__test.res diff --git a/src/FetchAPI/URLSearchParams.res b/src/FetchAPI/URLSearchParams.res index f78bc92..89bf4a3 100644 --- a/src/FetchAPI/URLSearchParams.res +++ b/src/FetchAPI/URLSearchParams.res @@ -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) @@ -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 = "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) @@ -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 = "values" diff --git a/tests/FetchAPI/URLSearchParams__test.js b/tests/FetchAPI/URLSearchParams__test.js new file mode 100644 index 0000000..4ee1bca --- /dev/null +++ b/tests/FetchAPI/URLSearchParams__test.js @@ -0,0 +1,30 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +let params1 = new URLSearchParams("foo=1&bar=2"); + +Array.from(params1.keys()).forEach(prim => { + console.log(prim); +}); + +let params2 = new URLSearchParams("?foo=1&bar=b"); + +Array.from(params2.values()).forEach(prim => { + console.log(prim); +}); + +let params3 = new URLSearchParams("?foo=1&bar=b"); + +Array.from(params3.entries()).forEach(param => { + console.log(param[0], param[1]); +}); + +let paramStr = params3.toString(); + +export { + params1, + params2, + params3, + paramStr, +} +/* params1 Not a pure module */ diff --git a/tests/FetchAPI/URLSearchParams__test.res b/tests/FetchAPI/URLSearchParams__test.res new file mode 100644 index 0000000..00ef3db --- /dev/null +++ b/tests/FetchAPI/URLSearchParams__test.res @@ -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