-
Notifications
You must be signed in to change notification settings - Fork 576
/
dirname.ts
executable file
·34 lines (31 loc) · 1.02 KB
/
dirname.ts
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
29
30
31
32
33
34
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { dirname as posixDirname } from "../path/posix/dirname.ts";
import { strip } from "./_strip.ts";
/**
* Returns the directory path of a URL or URL string.
*
* The directory path is the portion of a URL up to but excluding the final path
* segment.
*
* The final path segment, along with any query or hash values are
* removed. If there is no path segment then the URL origin is returned.
*
* @param url URL to extract the directory from.
* @returns The directory path of the URL.
*
* @example Basic usage
* ```ts
* import { dirname } from "https://deno.land/std@$STD_VERSION/url/dirname.ts";
*
* dirname("https://deno.land/std/path/mod.ts?a=b").href; // "https://deno.land/std/path"
*
* dirname("https://deno.land/").href; // "https://deno.land"
* ```
*/
export function dirname(url: string | URL): URL {
url = new URL(url);
strip(url);
url.pathname = posixDirname(url.pathname);
return url;
}