@oazmi/build-tools
Version:
general deno build tool scripts which I practically use in all of my typescript repos
30 lines (29 loc) • 1.29 kB
JavaScript
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { assertArgs, lastPathSegment, stripSuffix, } from "../_common/basename.js";
import { stripTrailingSeparators } from "../_common/strip_trailing_separators.js";
import { isPosixPathSeparator } from "./_util.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/posix/basename";
* import { assertEquals } from "@std/assert/assert-equals";
*
* assertEquals(basename("/home/user/Documents/"), "Documents");
* assertEquals(basename("/home/user/Documents/image.png"), "image.png");
* assertEquals(basename("/home/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 = "") {
assertArgs(path, suffix);
const lastSegment = lastPathSegment(path, isPosixPathSeparator);
const strippedSegment = stripTrailingSeparators(lastSegment, isPosixPathSeparator);
return suffix ? stripSuffix(strippedSegment, suffix) : strippedSegment;
}