yahoo-finance2
Version:
JS API for Yahoo Finance
56 lines (55 loc) • 2.56 kB
JavaScript
;
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });
exports.basename = basename;
const basename_js_1 = require("../_common/basename.js");
const from_file_url_js_1 = require("./from_file_url.js");
const strip_trailing_separators_js_1 = require("../_common/strip_trailing_separators.js");
const _util_js_1 = require("./_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";
*
* 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
*
* 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.
*
* ```ts
* import { basename } from "@std/path/posix/basename";
* import { assertEquals } from "@std/assert";
*
* assertEquals(basename("https://deno.land/std/path/mod.ts"), "mod.ts");
* assertEquals(basename("https://deno.land/std/path/mod.ts", ".ts"), "mod");
* assertEquals(basename("https://deno.land/std/path/mod.ts?a=b"), "mod.ts?a=b");
* assertEquals(basename("https://deno.land/std/path/mod.ts#header"), "mod.ts#header");
* ```
*
* @param path The path to extract the name from.
* @param suffix The suffix to remove from extracted name.
* @returns The extracted name.
*/
function basename(path, suffix = "") {
if (path instanceof URL) {
path = (0, from_file_url_js_1.fromFileUrl)(path);
}
(0, basename_js_1.assertArgs)(path, suffix);
const lastSegment = (0, basename_js_1.lastPathSegment)(path, _util_js_1.isPosixPathSeparator);
const strippedSegment = (0, strip_trailing_separators_js_1.stripTrailingSeparators)(lastSegment, _util_js_1.isPosixPathSeparator);
return suffix ? (0, basename_js_1.stripSuffix)(strippedSegment, suffix) : strippedSegment;
}