-
Notifications
You must be signed in to change notification settings - Fork 632
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(std): add
std/url
module. (#3527)
- Loading branch information
Showing
11 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { posixBasename } from "../path/_basename.ts"; | ||
|
||
/** | ||
* Return the last portion of a `url`. | ||
* Trailing `/`s are ignored, and optional suffix is removed. | ||
* | ||
* @param url - url to extract the name from. | ||
* @param [suffix] - suffix to remove from extracted name. | ||
*/ | ||
export function basename(url: string | URL, suffix?: string) { | ||
url = new URL(url); | ||
const basename = posixBasename(url.pathname, suffix); | ||
return basename === "/" ? url.origin : basename; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../assert/mod.ts"; | ||
import * as url from "./mod.ts"; | ||
|
||
const TESTSUITE: [[string | URL, string?], string][] = [ | ||
[["https://deno.land/std/assert/mod.ts"], "mod.ts"], | ||
[[new URL("https://deno.land/std/assert/mod.ts")], "mod.ts"], | ||
[[new URL("https://deno.land/std/assert/mod.ts"), ".ts"], "mod"], | ||
[[new URL("https://deno.land/std/assert/mod.ts?foo=bar")], "mod.ts"], | ||
[[new URL("https://deno.land/std/assert/mod.ts#header")], "mod.ts"], | ||
]; | ||
|
||
Deno.test("basename", function () { | ||
for (const [[test_url, suffix], expected] of TESTSUITE) { | ||
assertEquals(url.basename(test_url, suffix), expected); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { posixDirname } from "../path/_dirname.ts"; | ||
|
||
/** | ||
* Return the directory path of a `url`. | ||
* @param url - url to extract the directory from. | ||
*/ | ||
export function dirname(url: string | URL) { | ||
url = new URL(url); | ||
url.pathname = posixDirname(url.pathname); | ||
url.search = ""; | ||
url.hash = ""; | ||
return url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../assert/mod.ts"; | ||
import * as url from "./mod.ts"; | ||
|
||
const TESTSUITE = [ | ||
[ | ||
"https://deno.land/std/assert/mod.ts", | ||
new URL("https://deno.land/std/assert"), | ||
], | ||
[ | ||
new URL("https://deno.land/std/assert/mod.ts"), | ||
new URL("https://deno.land/std/assert"), | ||
], | ||
[ | ||
new URL("https://deno.land/std/assert/mod.ts?foo=bar"), | ||
new URL("https://deno.land/std/assert"), | ||
], | ||
[ | ||
new URL("https://deno.land/std/assert/mod.ts#header"), | ||
new URL("https://deno.land/std/assert"), | ||
], | ||
[new URL("https://deno.land///"), new URL("https://deno.land")], | ||
]; | ||
|
||
Deno.test("dirname", function () { | ||
for (const [test_url, expected] of TESTSUITE) { | ||
assertEquals(url.dirname(test_url), expected); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { posixExtname } from "../path/_extname.ts"; | ||
|
||
/** | ||
* Return the extension of the `url` with leading period. | ||
* @param url with extension | ||
* @returns extension (ex. for url ending with `index.html` returns `.html`) | ||
*/ | ||
export function extname(url: string | URL) { | ||
url = new URL(url); | ||
return posixExtname(url.pathname); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../assert/mod.ts"; | ||
import * as url from "./mod.ts"; | ||
|
||
const TESTSUITE = [ | ||
["https://deno.land/std/assert/mod.ts", ".ts"], | ||
[new URL("https://deno.land/std/assert/mod.ts"), ".ts"], | ||
[new URL("https://deno.land/std/assert/mod.ts?foo=bar"), ".ts"], | ||
[new URL("https://deno.land/std/assert/mod.ts#header"), ".ts"], | ||
[new URL("https://deno.land/std/assert/mod."), "."], | ||
[new URL("https://deno.land/std/assert/mod"), ""], | ||
]; | ||
|
||
Deno.test("extname", function () { | ||
for (const [test_url, expected] of TESTSUITE) { | ||
assertEquals(url.extname(test_url), expected); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { normalize } from "./normalize.ts"; | ||
|
||
/** | ||
* Join all given a sequence of a `url` and `paths`,then normalizes the resulting url. | ||
* @param url to be joined with the paths and normalized | ||
* @param paths to be joined | ||
*/ | ||
export function join(url: string | URL, ...paths: string[]) { | ||
return normalize(url.toString() + "/" + paths.join("/")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../assert/mod.ts"; | ||
import * as url from "./mod.ts"; | ||
|
||
const TESTSUITE: [[string | URL, ...string[]], URL][] = [ | ||
[ | ||
["https://deno.land", "std", "assert", "mod.ts"], | ||
new URL("https://deno.land/std/assert/mod.ts"), | ||
], | ||
[ | ||
[new URL("https://deno.land"), "std", "assert", "mod.ts"], | ||
new URL("https://deno.land/std/assert/mod.ts"), | ||
], | ||
[ | ||
[new URL("https://deno.land//"), "/std/", "/assert/", "//mod.ts"], | ||
new URL("https://deno.land/std/assert/mod.ts"), | ||
], | ||
[["https://deno.land/"], new URL("https://deno.land/")], | ||
]; | ||
|
||
Deno.test("join", function () { | ||
for (const [[test_url, ...paths], expected] of TESTSUITE) { | ||
assertEquals(url.join(test_url, ...paths), expected); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
/** | ||
* Utilities for working with URL paths. | ||
* | ||
* This module is browser compatible. | ||
* | ||
* @module | ||
*/ | ||
|
||
export * from "./basename.ts"; | ||
export * from "./dirname.ts"; | ||
export * from "./extname.ts"; | ||
export * from "./join.ts"; | ||
export * from "./normalize.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
// This module is browser compatible. | ||
|
||
import { posixNormalize } from "../path/_normalize.ts"; | ||
|
||
/** | ||
* Normalize the `url`, resolving `'..'` and `'.'` segments and multiple `'/'`s into `'//'` after protocol and remaining into `'/'`. | ||
* @param url to be normalized | ||
*/ | ||
export function normalize(url: string | URL) { | ||
url = new URL(url); | ||
url.pathname = posixNormalize(url.pathname); | ||
return url; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. | ||
|
||
import { assertEquals } from "../assert/mod.ts"; | ||
import * as url from "./mod.ts"; | ||
|
||
const TESTSUITE = [ | ||
[ | ||
"https:///deno.land///std//assert////mod.ts", | ||
new URL("https://deno.land/std/assert/mod.ts"), | ||
], | ||
[ | ||
"https://deno.land///std//assert////mod.ts?foo=bar", | ||
new URL("https://deno.land/std/assert/mod.ts?foo=bar"), | ||
], | ||
[ | ||
"https://deno.land///std//assert////mod.ts#header", | ||
new URL("https://deno.land/std/assert/mod.ts#header"), | ||
], | ||
[ | ||
new URL("https://deno.land/std/assert/../async/retry.ts"), | ||
new URL("https://deno.land/std/async/retry.ts"), | ||
], | ||
["https:/deno.land//..", new URL("https://deno.land")], | ||
]; | ||
|
||
Deno.test("normalize", function () { | ||
for (const [test_url, expected] of TESTSUITE) { | ||
assertEquals(url.normalize(test_url), expected); | ||
} | ||
}); |