summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
authorSven Nicolai Viig <sven.viig@gmail.com>2019-10-31 19:55:54 +0100
committerRy Dahl <ry@tinyclouds.org>2019-10-31 14:55:54 -0400
commitd7a5aed5114f3ffd27221ae97bc57f453410427e (patch)
tree7f7a9fc3e14a7597efe2777ba35889adae01943b /cli/js
parent4f8c9369744bfa4b1223cec916cb0e5b261831dc (diff)
Adds custom inspect method for URL (#3241)
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/url.ts21
-rw-r--r--cli/js/url_test.ts8
2 files changed, 29 insertions, 0 deletions
diff --git a/cli/js/url.ts b/cli/js/url.ts
index f22198da4..7b06fd64c 100644
--- a/cli/js/url.ts
+++ b/cli/js/url.ts
@@ -3,6 +3,7 @@ import * as urlSearchParams from "./url_search_params.ts";
import * as domTypes from "./dom_types.ts";
import { getRandomValues } from "./get_random_values.ts";
import { window } from "./window.ts";
+import { customInspect } from "./console.ts";
interface URLParts {
protocol: string;
@@ -144,6 +145,26 @@ export class URL {
private _parts: URLParts;
private _searchParams!: urlSearchParams.URLSearchParams;
+ [customInspect](): string {
+ const keys = [
+ "href",
+ "origin",
+ "protocol",
+ "username",
+ "password",
+ "host",
+ "hostname",
+ "port",
+ "pathname",
+ "hash",
+ "search"
+ ];
+ const objectString = keys
+ .map((key: string) => `${key}: "${this[key] || ""}"`)
+ .join(", ");
+ return `URL { ${objectString} }`;
+ }
+
private _updateSearchParams(): void {
const searchParams = new urlSearchParams.URLSearchParams(this.search);
diff --git a/cli/js/url_test.ts b/cli/js/url_test.ts
index 07a8028ce..23f9f5f4b 100644
--- a/cli/js/url_test.ts
+++ b/cli/js/url_test.ts
@@ -179,3 +179,11 @@ test(function sortingNonExistentParamRemovesQuestionMarkFromURL(): void {
assertEquals(url.href, "http://example.com/");
assertEquals(url.search, "");
});
+
+test(function customInspectFunction(): void {
+ const url = new URL("http://example.com/?");
+ assertEquals(
+ Deno.inspect(url),
+ 'URL { href: "http://example.com/?", origin: "http://example.com", protocol: "http:", username: "", password: "", host: "example.com", hostname: "example.com", port: "", pathname: "/", hash: "", search: "?" }'
+ );
+});