mirror of
https://github.com/denoland/deno.git
synced 2025-01-08 15:19:40 -05:00
Adds custom inspect method for URL (#3241)
This commit is contained in:
parent
4f8c936974
commit
d7a5aed511
2 changed files with 29 additions and 0 deletions
|
@ -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);
|
||||
|
||||
|
|
|
@ -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: "?" }'
|
||||
);
|
||||
});
|
||||
|
|
Loading…
Reference in a new issue