summaryrefslogtreecommitdiff
path: root/tests/registry/jsr/@std/url/0.220.1/join.ts
blob: b9c8f19d31a648708e0561484d8a7e5d33eaab41 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

import { join as posixJoin } from "jsr:@std/path@^0.220.1/posix/join";

/**
 * Join a base `URL` and a series of `paths`, then normalizes the resulting URL.
 *
 * @example
 * ```ts
 * import { join } from "@std/url/join";
 *
 * console.log(join("https://deno.land/", "std", "path", "mod.ts").href);
 * // Outputs: "https://deno.land/std/path/mod.ts"
 *
 * console.log(join("https://deno.land", "//std", "path/", "/mod.ts").href);
 * // Outputs: "https://deno.land/path/mod.ts"
 * ```
 *
 * @param url the 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 string containing the base URL joined with the paths
 */
export function join(url: string | URL, ...paths: string[]): URL {
  url = new URL(url);
  url.pathname = posixJoin(url.pathname, ...paths);
  return url;
}