2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-09-01 08:08:15 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
2024-04-29 02:57:30 +00:00
|
|
|
import { join as posixJoin } from "@std/path/posix/join";
|
2023-09-01 08:08:15 +00:00
|
|
|
|
|
|
|
/**
|
2024-04-17 04:49:49 +00:00
|
|
|
* Joins a base URL or URL string, and a sequence of path segments together,
|
|
|
|
* then normalizes the resulting URL.
|
2023-09-04 09:12:28 +00:00
|
|
|
*
|
2024-04-17 04:49:49 +00:00
|
|
|
* @param url Base URL to be joined with the paths and normalized.
|
|
|
|
* @param paths Array of path segments to be joined to the base URL.
|
|
|
|
* @returns A complete URL containing the base URL joined with the paths.
|
|
|
|
*
|
2024-06-05 21:16:52 +00:00
|
|
|
* @example Usage
|
|
|
|
*
|
2023-09-04 09:12:28 +00:00
|
|
|
* ```ts
|
2024-04-29 02:57:30 +00:00
|
|
|
* import { join } from "@std/url/join";
|
refactor(assert,async,bytes,cli,collections,crypto,csv,data-structures,datetime,dotenv,encoding,expect,fmt,front-matter,fs,html,http,ini,internal,io,json,jsonc,log,media-types,msgpack,net,path,semver,streams,testing,text,toml,ulid,url,uuid,webgpu,yaml): import from `@std/assert` (#5199)
* refactor: import from `@std/assert`
* update
2024-06-30 08:30:10 +00:00
|
|
|
* import { assertEquals } from "@std/assert";
|
2023-09-04 09:12:28 +00:00
|
|
|
*
|
2024-06-05 21:16:52 +00:00
|
|
|
* assertEquals(join("https://deno.land/", "std", "path", "mod.ts").href, "https://deno.land/std/path/mod.ts");
|
|
|
|
* assertEquals(join("https://deno.land", "//std", "path/", "/mod.ts").href, "https://deno.land/std/path/mod.ts");
|
2023-09-04 09:12:28 +00:00
|
|
|
* ```
|
2024-08-02 02:53:11 +00:00
|
|
|
*
|
|
|
|
* @deprecated Use
|
|
|
|
* {@linkcode https://jsr.io/@std/path/doc/posix/~/join | @std/path/posix/join}
|
|
|
|
* instead (examples included). `@std/url` will be removed in the future.
|
2023-09-01 08:08:15 +00:00
|
|
|
*/
|
2023-09-04 09:12:28 +00:00
|
|
|
export function join(url: string | URL, ...paths: string[]): URL {
|
2023-09-03 01:40:08 +00:00
|
|
|
url = new URL(url);
|
|
|
|
url.pathname = posixJoin(url.pathname, ...paths);
|
|
|
|
return url;
|
2023-09-01 08:08:15 +00:00
|
|
|
}
|