extname

import { extname } from "jsr:@ende/path/posix/extname";

function

function extname(path: string | URL): string

Return the extension of the path with leading period.

path: string | URL

The path to get the extension from.

Returns

The extension (ex. for file.ts returns .ts).

Example: Usage

import { extname } from "@ende/path/posix/extname";
import { assertEquals } from "@ende/assert";

assertEquals(extname("/home/user/Documents/file.ts"), ".ts");
assertEquals(extname("/home/user/Documents/"), "");
assertEquals(extname("/home/user/Documents/image.png"), ".png");
assertEquals(extname(new URL("file:///home/user/Documents/file.ts")), ".ts");
assertEquals(extname(new URL("file:///home/user/Documents/file.ts?a=b")), ".ts");
assertEquals(extname(new URL("file:///home/user/Documents/file.ts#header")), ".ts");

Example: Working with URLs

Only URL instances with the file: protocol are accepted. To process a non-file: URL, pass it as a string or pass its pathname property.

Note: This function doesn't automatically strip hash and query parts from URLs. If your URL contains a hash or query, remove them before passing the URL to the function. This can be done by passing the URL to new URL(url), and setting the hash and search properties to empty strings.

import { extname } from "@ende/path/posix/extname";
import { assertEquals } from "@ende/assert";

assertEquals(extname("https://ende.sh/std/path/mod.ts"), ".ts");
assertEquals(extname("https://ende.sh/std/path/mod.ts?a=b"), ".ts?a=b");
assertEquals(extname("https://ende.sh/std/path/mod.ts#header"), ".ts#header");
assertEquals(extname(new URL("https://ende.sh/std/path/mod.ts").pathname), ".ts");

Source