std/url/extname.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 { extname as posixExtname } from "@std/path/posix/extname";
import { strip } from "./_strip.ts";
/**
* Returns the file extension of a given URL or string with leading period.
*
* The extension is sourced from the path portion of the URL. If there is no
* extension, an empty string is returned. URL queries and hashes are ignored.
*
* @param url The URL from which to extract the extension.
* @returns The extension of the URL.
*
* @example Usage
* ```ts
* import { extname } from "@std/url/extname";
* import { assertEquals } from "@std/assert";
*
* assertEquals(extname("https://deno.land/std/path/mod.ts"), ".ts");
* assertEquals(extname("https://deno.land/std/path/mod"), "");
* assertEquals(extname("https://deno.land/std/path/mod.ts?a=b"), ".ts");
* assertEquals(extname("https://deno.land/"), "");
* ```
*
* @deprecated Use
* {@linkcode https://jsr.io/@std/path/doc/posix/~/extname | @std/path/posix/extname}
* instead (examples included). `@std/url` will be removed in the future.
*/
export function extname(url: string | URL): string {
url = new URL(url);
strip(url);
return posixExtname(url.pathname);
}