Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(path/unstable): support URL in first arg of join() #5863

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions path/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,26 @@ import { join as windowsJoin } from "./windows/join.ts";
* @param paths Paths to be joined and normalized.
* @returns The joined and normalized path.
*/
export function join(...paths: string[]): string {
return isWindows ? windowsJoin(...paths) : posixJoin(...paths);
export function join(...paths: string[]): string;
/**
* Join all given a sequence of `paths`, then normalizes the resulting path.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { join } from "@std/path/posix/join";
* import { assertEquals } from "@std/assert";
*
* const path = join(new URL("file:///foo"), "bar", "baz/asdf", "quux", "..");
* assertEquals(path, "/foo/bar/baz/asdf");
* ```
*
* @param path The path to join. This can be string or file URL.
* @param paths The paths to join.
* @returns The joined path.
*/
export function join(path?: URL | string, ...paths: string[]): string;
export function join(path?: URL | string, ...paths: string[]): string {
return isWindows ? windowsJoin(path, ...paths) : posixJoin(path, ...paths);
}
24 changes: 19 additions & 5 deletions path/join_test.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current tests seem a little bare. Could we please add the tests from the examples?

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { join } from "./join.ts";

const backslashRE = /\\/g;

const joinTests =
type TestCase = [string[] | [URL, ...string[]], string];

const joinTests: TestCase[] =
// arguments result
[
[[".", "x/b", "..", "/b/c.js"], "x/b/c.js"],
Expand Down Expand Up @@ -56,10 +58,18 @@ const joinTests =
[["/", "", "/foo"], "/foo"],
[["", "/", "foo"], "/foo"],
[["", "/", "/foo"], "/foo"],

// URLs
[[new URL("file:///"), "x/b", "..", "/b/c.js"], "/x/b/c.js"],
[[new URL("file:///foo"), "../../../bar"], "/bar"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The first test case looks good. But I'd expect the result of the 2nd test case to be "/../../bar".

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/bar is the normalized form of /../../bar. There's no parent directory of the root directory

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I tested with node:path and that does appear to be the case. That doesn't really make sense to me but I guess is fine 🤷🏾‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did you test it? The below prints /bar both in Node.js and Deno

import { join } from "node:path";
console.log(join("/foo", "../../../bar"));

BTW join("file:///foo", "../../../bar") becomes ../bar because "file:///foo" is considered relative path file:/bar (file: part is considered as directory name instead of scheme name)

[
[new URL("file:///foo"), "bar", "baz/asdf", "quux", ".."],
"/foo/bar/baz/asdf",
],
];

// Windows-specific join tests
const windowsJoinTests = [
const windowsJoinTests: TestCase[] = [
// arguments result
// UNC path expected
[["//foo/bar"], "\\\\foo\\bar\\"],
Expand Down Expand Up @@ -106,24 +116,28 @@ const windowsJoinTests = [
[["c:.", "file"], "c:file"],
[["c:", "/"], "c:\\"],
[["c:", "file"], "c:\\file"],
// URLs
[[new URL("file:///c:")], "c:\\"],
[[new URL("file:///c:"), "file"], "c:\\file"],
[[new URL("file:///c:/"), "file"], "c:\\file"],
];

Deno.test("posix.join()", function () {
joinTests.forEach(function (p) {
const _p = p[0] as string[];
const _p = p[0];
const actual = posix.join.apply(null, _p);
assertEquals(actual, p[1]);
});
});

Deno.test("windows.join()", function () {
joinTests.forEach(function (p) {
const _p = p[0] as string[];
const _p = p[0];
const actual = windows.join.apply(null, _p).replace(backslashRE, "/");
assertEquals(actual, p[1]);
});
windowsJoinTests.forEach(function (p) {
const _p = p[0] as string[];
const _p = p[0];
const actual = windows.join.apply(null, _p);
assertEquals(actual, p[1]);
});
Expand Down
28 changes: 19 additions & 9 deletions path/posix/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import { assertPath } from "../_common/assert_path.ts";
import { normalize } from "./normalize.ts";
import { fromFileUrl } from "./from_file_url.ts";

/**
* Join all given a sequence of `paths`,then normalizes the resulting path.
Expand All @@ -16,24 +17,33 @@ import { normalize } from "./normalize.ts";
* assertEquals(path, "/foo/bar/baz/asdf");
* ```
*
* @example Working with URLs
* @param paths The paths to join.
* @returns The joined path.
*/
export function join(...paths: string[]): string;
/**
* Join all given a sequence of `paths`, then normalizes the resulting path.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { join } from "@std/path/posix/join";
* import { assertEquals } from "@std/assert";
*
* const url = new URL("https://deno.land");
* url.pathname = join("std", "path", "mod.ts");
* assertEquals(url.href, "https://deno.land/std/path/mod.ts");
*
* url.pathname = join("//std", "path/", "/mod.ts");
* assertEquals(url.href, "https://deno.land/std/path/mod.ts");
* const path = join(new URL("file:///foo"), "bar", "baz/asdf", "quux", "..");
* assertEquals(path, "/foo/bar/baz/asdf");
* ```
*
* @param path The path to join. This can be string or file URL.
* @param paths The paths to join.
* @returns The joined path.
*/
export function join(...paths: string[]): string {
if (paths.length === 0) return ".";
export function join(path?: URL | string, ...paths: string[]): string;
export function join(path?: URL | string, ...paths: string[]): string {
if (path === undefined) return ".";
path = path instanceof URL ? fromFileUrl(path) : path;
paths = path ? [path, ...paths] : paths;
paths.forEach((path) => assertPath(path));
const joined = paths.filter((path) => path.length > 0).join("/");
return joined === "" ? "." : normalize(joined);
Expand Down
25 changes: 24 additions & 1 deletion path/windows/join.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import { assertPath } from "../_common/assert_path.ts";
import { isPathSeparator } from "./_util.ts";
import { normalize } from "./normalize.ts";
import { fromFileUrl } from "./from_file_url.ts";

/**
* Join all given a sequence of `paths`,then normalizes the resulting path.
Expand All @@ -20,7 +21,29 @@ import { normalize } from "./normalize.ts";
* @param paths The paths to join.
* @returns The joined path.
*/
export function join(...paths: string[]): string {
export function join(...paths: string[]): string;
/**
* Join all given a sequence of `paths`, then normalizes the resulting path.
*
* @experimental **UNSTABLE**: New API, yet to be vetted.
*
* @example Usage
* ```ts
* import { join } from "@std/path/windows/join";
* import { assertEquals } from "@std/assert";
*
* const joined = join(new URL("file:///C:/foo"), "bar", "baz\\..");
* assertEquals(joined, "C:\\foo\\bar");
* ```
*
* @param path The path to join. This can be string or file URL.
* @param paths The paths to join.
* @returns The joined path.
*/
export function join(path?: URL | string, ...paths: string[]): string;
export function join(path?: URL | string, ...paths: string[]): string {
path = path instanceof URL ? fromFileUrl(path) : path;
paths = path ? [path, ...paths] : paths;
paths.forEach((path) => assertPath(path));
paths = paths.filter((path) => path.length > 0);
if (paths.length === 0) return ".";
Expand Down