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

Change setSearch to handle SearchParams.t #176

Merged
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
3 changes: 2 additions & 1 deletion packages/url/URL.rei
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ let setPort: (t, string) => t;
let protocol: t => option(string);
let setProtocol: (t, string) => t;
let search: t => option(string);
let setSearch: (t, string) => t;
let setSearchAsString: (t, string) => t;
let setSearch: (t, SearchParams.t) => t;
let searchParams: t => SearchParams.t;
let username: t => option(string);
let setUsername: (t, string) => t;
Expand Down
8 changes: 6 additions & 2 deletions packages/url/js/URL.re
Original file line number Diff line number Diff line change
Expand Up @@ -192,11 +192,15 @@ let search = url => {
};
};
[@mel.set] external setSearchInPlace: (t, string) => unit = "search";
let setSearch = (url, newSearch) => {
let setSearchAsString = (url, searchString) => {
let newUrl = makeExn(toString(url));
let () = setSearchInPlace(newUrl, newSearch);
let () = setSearchInPlace(newUrl, searchString);
newUrl;
};
let setSearch = (url, searchParams) => {
let queryString = SearchParams.toString(searchParams);
setSearchAsString(url, queryString);
};

[@mel.get] external getUsername: t => string = "username";
let username = url => {
Expand Down
5 changes: 3 additions & 2 deletions packages/url/native/URL.re
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,10 @@ let search = url => {
| _ => Some("?" ++ Uri.encoded_of_query(~scheme, query))
};
};
let setSearch = (t, string) => {
Uri.with_query(t, Uri.query_of_encoded(string));
let setSearchAsString = (t, searchString) => {
Uri.with_query(t, Uri.query_of_encoded(searchString));
};
let setSearch = Uri.with_query;
let searchParams = _url => assert(false);
let username = url => {
switch (Uri.user(url)) {
Expand Down
12 changes: 11 additions & 1 deletion packages/url/test/test_native.re
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,18 @@ let url_tests = (
assert_option_string(URL.search(url), Some("?lang=en"));
let url = URL.makeExn("https://www.google.es?lang=en&region=cat");
assert_option_string(URL.search(url), Some("?lang=en&region=cat"));
let url = URL.setSearch(url, "x=1&y=2");
let url = URL.setSearchAsString(url, "x=1&y=2");
assert_string(URL.toString(url), "https://www.google.es?x=1&y=2");
let search_params =
URL.SearchParams.makeWithArray([|
("name", "John"),
("last_name", "Doe"),
|]);
let url = URL.setSearch(url, search_params);
assert_string(
URL.toString(url),
"https://www.google.es?name=John&last_name=Doe",
);
}),
case("protocol", () => {
let url = URL.makeExn("//cdn.example.com/somewhere/something.js");
Expand Down
Loading