std/url/dirname.ts
Asher Gomez 3f133951f6
deprecation(url): deprecate @std/url (#5530)
chore(url): deprecate `@std/url`
2024-08-02 12:53:11 +10:00

36 lines
1.2 KiB
TypeScript
Executable File

// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { dirname as posixDirname } from "@std/path/posix/dirname";
import { strip } from "./_strip.ts";
/**
* Returns the directory path URL of a URL or URL string.
*
* The directory path is the portion of a URL up to but excluding the final path
* segment. URL queries and hashes are ignored.
*
* @param url URL to extract the directory from.
* @returns The directory path URL of the URL.
*
* @example Usage
* ```ts
* import { dirname } from "@std/url/dirname";
* import { assertEquals } from "@std/assert";
*
* assertEquals(dirname("https://deno.land/std/path/mod.ts"), new URL("https://deno.land/std/path"));
* assertEquals(dirname(new URL("https://deno.land/std/path/mod.ts")), new URL("https://deno.land/std/path"));
* ```
*
* @deprecated Use
* {@linkcode https://jsr.io/@std/path/doc/posix/~/dirname | @std/path/posix/dirname}
* instead (examples included). `@std/url` will be removed in the future.
*/
export function dirname(url: string | URL): URL {
url = new URL(url);
strip(url);
url.pathname = posixDirname(url.pathname);
return url;
}