basename

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

function

function basename(path: string | URL, suffix: string = ): string

Return the last portion of a path.

The trailing directory separators are ignored, and optional suffix is removed.

path: string | URL

Path to extract the name from. When passed as a URL instance, its protocol must be file:. For other protocols, pass the URL as a string or pass its pathname property.

suffix: string =

Suffix to remove from extracted name.

Returns

The basename of the path.

Example: Usage

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

if (Ende.build.os === "windows") {
  assertEquals(basename("C:\\user\\Documents\\image.png"), "image.png");
  assertEquals(basename(new URL("file:///C:/user/Documents/image.png")), "image.png");
} else {
  assertEquals(basename("/home/user/Documents/image.png"), "image.png");
  assertEquals(basename(new URL("file:///home/user/Documents/image.png")), "image.png");
}

Source