basename
import { basename } from "jsr:@ende/path/posix/basename";function
function basename(path: string | URL, suffix: string = ): stringReturn the last portion of a path.
Trailing directory separators are ignored, and optional suffix is removed.
path: string | URLThe path to extract the name from.
suffix: string =The suffix to remove from extracted name.
Returns
The extracted name.
Example: Usage
import { basename } from "@ende/path/posix/basename";
import { assertEquals } from "@ende/assert";
assertEquals(basename("/home/user/Documents/"), "Documents");
assertEquals(basename("/home/user/Documents/image.png"), "image.png");
assertEquals(basename("/home/user/Documents/image.png", ".png"), "image");
assertEquals(basename(new URL("file:///home/user/Documents/image.png")), "image.png");
assertEquals(basename(new URL("file:///home/user/Documents/image.png"), ".png"), "image");
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 { basename } from "@ende/path/posix/basename";
import { assertEquals } from "@ende/assert";
assertEquals(basename("https://ende.sh/std/path/mod.ts"), "mod.ts");
assertEquals(basename("https://ende.sh/std/path/mod.ts", ".ts"), "mod");
assertEquals(basename("https://ende.sh/std/path/mod.ts?a=b"), "mod.ts?a=b");
assertEquals(basename("https://ende.sh/std/path/mod.ts#header"), "mod.ts#header");
assertEquals(basename(new URL("https://ende.sh/std/path/mod.ts").pathname), "mod.ts");