UNPKG

@lonu/stc

Version:

A tool for converting OpenApi/Swagger/Apifox into code.

48 lines (47 loc) 2.02 kB
// Copyright 2018-2025 the Deno authors. MIT license. // This module is browser compatible. import { assertArgs, lastPathSegment, stripSuffix, } from "../_common/basename.js"; import { CHAR_COLON } from "../_common/constants.js"; import { stripTrailingSeparators } from "../_common/strip_trailing_separators.js"; import { isPathSeparator, isWindowsDeviceRoot } from "./_util.js"; import { fromFileUrl } from "./from_file_url.js"; /** * Return the last portion of a `path`. * Trailing directory separators are ignored, and optional suffix is removed. * * @example Usage * ```ts * import { basename } from "@std/path/windows/basename"; * import { assertEquals } from "@std/assert"; * * assertEquals(basename("C:\\user\\Documents\\"), "Documents"); * assertEquals(basename("C:\\user\\Documents\\image.png"), "image.png"); * assertEquals(basename("C:\\user\\Documents\\image.png", ".png"), "image"); * assertEquals(basename(new URL("file:///C:/user/Documents/image.png")), "image.png"); * assertEquals(basename(new URL("file:///C:/user/Documents/image.png"), ".png"), "image"); * ``` * * @param path The path to extract the name from. * @param suffix The suffix to remove from extracted name. * @returns The extracted name. */ export function basename(path, suffix = "") { if (path instanceof URL) { path = fromFileUrl(path); } assertArgs(path, suffix); // Check for a drive letter prefix so as not to mistake the following // path separator as an extra separator at the end of the path that can be // disregarded let start = 0; if (path.length >= 2) { const drive = path.charCodeAt(0); if (isWindowsDeviceRoot(drive)) { if (path.charCodeAt(1) === CHAR_COLON) start = 2; } } const lastSegment = lastPathSegment(path, isPathSeparator, start); const strippedSegment = stripTrailingSeparators(lastSegment, isPathSeparator); return suffix ? stripSuffix(strippedSegment, suffix) : strippedSegment; }