std/path/join.ts
Yoshiya Hinosawa 94a7e1b34b
feat(path/unstable): support URL in first arg of join() (#5863)
Co-authored-by: Asher Gomez <ashersaupingomez@gmail.com>
2024-08-30 13:18:12 +09:00

49 lines
1.6 KiB
TypeScript

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { isWindows } from "./_os.ts";
import { join as posixJoin } from "./posix/join.ts";
import { join as windowsJoin } from "./windows/join.ts";
/**
* Joins a sequence of paths, then normalizes the resulting path.
*
* @example Usage
* ```ts
* import { join } from "@std/path/join";
* import { assertEquals } from "@std/assert";
*
* if (Deno.build.os === "windows") {
* assertEquals(join("C:\\foo", "bar", "baz\\quux", "garply", ".."), "C:\\foo\\bar\\baz\\quux");
* } else {
* assertEquals(join("/foo", "bar", "baz/quux", "garply", ".."), "/foo/bar/baz/quux");
* }
* ```
*
* @param paths Paths to be joined and normalized.
* @returns The joined and normalized 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 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);
}