Skip to content

Commit

Permalink
removes ? from URL when deleting all params (denoland#2217)
Browse files Browse the repository at this point in the history
  • Loading branch information
justjavac authored and ry committed Apr 29, 2019
1 parent 636827a commit 4dcdd88
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
19 changes: 15 additions & 4 deletions js/url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ interface URLParts {
hostname: string;
port: string;
path: string;
query: string;
query: string | null;
hash: string;
}

Expand Down Expand Up @@ -192,15 +192,26 @@ export class URL {
}

get search(): string {
if (this._parts.query === null || this._parts.query === "") {
return "";
}

return this._parts.query;
}

set search(value: string) {
value = String(value);
if (value.charAt(0) !== "?") {
value = `?${value}`;
let query: string | null;

if (value === "") {
query = null;
} else if (value.charAt(0) !== "?") {
query = `?${value}`;
} else {
query = value;
}
this._parts.query = value;

this._parts.query = query;
this._updateSearchParams();
}

Expand Down
17 changes: 17 additions & 0 deletions js/url_search_params.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { URL } from "./url";
import { requiredArguments } from "./util";

export class URLSearchParams {
private params: Array<[string, string]> = [];
private url: URL | null = null;

constructor(init: string | string[][] | Record<string, string> = "") {
if (typeof init === "string") {
Expand Down Expand Up @@ -30,6 +32,20 @@ export class URLSearchParams {
}
}

private updateSteps(): void {
if (this.url === null) {
return;
}

let query: string | null = this.toString();
if (query === "") {
query = null;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
(this.url as any)._parts.query = query;
}

/** Appends a specified key/value pair as a new search parameter.
*
* searchParams.append('name', 'first');
Expand All @@ -56,6 +72,7 @@ export class URLSearchParams {
i++;
}
}
this.updateSteps();
}

/** Returns all the values associated with a given search parameter
Expand Down
16 changes: 16 additions & 0 deletions js/url_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,19 @@ test(function urlBaseString(): void {
);
assertEquals(url.href, "https://foo:bar@baz.qat:8000/foo/bar?baz=foo#qux");
});

test(function deletingAllParamsRemovesQuestionMarkFromURL(): void {
const url = new URL("http://example.com/?param1&param2");
url.searchParams.delete("param1");
url.searchParams.delete("param2");
assertEquals(url.href, "http://example.com/");
assertEquals(url.search, "");
});

test(function removingNonExistentParamRemovesQuestionMarkFromURL(): void {
const url = new URL("http://example.com/?");
assertEquals(url.href, "http://example.com/?");
url.searchParams.delete("param1");
assertEquals(url.href, "http://example.com/");
assertEquals(url.search, "");
});

0 comments on commit 4dcdd88

Please sign in to comment.