Skip to content

Commit

Permalink
feat(std): add std/url module. (#3527)
Browse files Browse the repository at this point in the history
  • Loading branch information
tr1ckydev authored Sep 1, 2023
1 parent 911e110 commit 4e44fa7
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 0 deletions.
17 changes: 17 additions & 0 deletions url/basename.ts
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;
}
18 changes: 18 additions & 0 deletions url/basename_test.ts
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);
}
});
16 changes: 16 additions & 0 deletions url/dirname.ts
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;
}
30 changes: 30 additions & 0 deletions url/dirname_test.ts
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);
}
});
14 changes: 14 additions & 0 deletions url/extname.ts
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);
}
19 changes: 19 additions & 0 deletions url/extname_test.ts
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);
}
});
13 changes: 13 additions & 0 deletions url/join.ts
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("/"));
}
26 changes: 26 additions & 0 deletions url/join_test.ts
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);
}
});
16 changes: 16 additions & 0 deletions url/mod.ts
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";
14 changes: 14 additions & 0 deletions url/normalize.ts
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;
}
30 changes: 30 additions & 0 deletions url/normalize_test.ts
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);
}
});

0 comments on commit 4e44fa7

Please sign in to comment.