Skip to content

Commit a2ba8b8

Browse files
committed
feat: url parse
1 parent 9ba2857 commit a2ba8b8

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

examples/url.ts

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,44 @@ console.log(B)
1010

1111
console.log(new URL("en-US/docs", B)); // 'https://developer.mozilla.org/en-US/docs'
1212

13-
1413
const D = new URL("/en-US/docs", B); // 'https://developer.mozilla.org/en-US/docs'
1514
console.log(D)
1615

1716
console.log(new URL("/en-US/docs", D)); // 'https://developer.mozilla.org/en-US/docs'
1817

1918
console.log(new URL("/en-US/docs", A)); // 'https://developer.mozilla.org/en-US/docs'
2019

21-
new URL("/en-US/docs", "https://developer.mozilla.org/fr-FR/toto"); // 'https://developer.mozilla.org/en-US/docs'
20+
new URL("/en-US/docs", "https://developer.mozilla.org/fr-FR/toto"); // 'https://developer.mozilla.org/en-US/docs'
21+
22+
// https://developer.mozilla.org/en-US/docs/Web/API/URL/parse_static#examples
23+
if ("parse" in URL) {
24+
// Absolute URL
25+
let result = URL.parse("https://developer.mozilla.org/en-US/docs");
26+
console.log(`[1]: ${result}`);
27+
28+
// Relative reference to a valid base URL
29+
result = URL.parse("en-US/docs", "https://developer.mozilla.org");
30+
console.log(`[2]: ${result}`);
31+
32+
// Relative reference to a "complicated" valid base URL
33+
// (only the scheme and domain are used to resolve url)
34+
result = URL.parse(
35+
"/different/place",
36+
"https://developer.mozilla.org:443/some/path?id=4",
37+
);
38+
console.log(`[3]: ${result}`);
39+
40+
// Absolute url argument (base URL ignored)
41+
result = URL.parse(
42+
"https://example.org/some/docs",
43+
"https://developer.mozilla.org",
44+
);
45+
console.log(`[4]: ${result}`);
46+
47+
// TODO: error is returned, but null should be returned here
48+
// Invalid base URL (missing colon)
49+
// result = URL.parse("en-US/docs", "https//developer.mozilla.org");
50+
// console.log(`[5]: ${result}`);
51+
} else {
52+
console.log("URL.parse() not supported");
53+
}

runtime/src/ext/url/mod.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// deno-lint-ignore-file no-unused-vars
22
class URL {
3+
// TODO: Need to return a URL object.
34
constructor(url: string, base?: string) {
45
// @ts-ignore - this is a hack to make the URL object work
56
this.url = url;
@@ -15,4 +16,8 @@ class URL {
1516
// @ts-ignore - this is a hack to make the URL object work
1617
return this.serialized;
1718
}
18-
}
19+
20+
static parse(url: string, base?: string) {
21+
return new this(url, base)
22+
}
23+
}

0 commit comments

Comments
 (0)