yahoo-finance2
Version:
JS API for Yahoo Finance
68 lines (67 loc) • 2.45 kB
JavaScript
;
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
Object.defineProperty(exports, "__esModule", { value: true });
exports.dirname = dirname;
const dirname_js_1 = require("../_common/dirname.js");
const strip_trailing_separators_js_1 = require("../_common/strip_trailing_separators.js");
const _util_js_1 = require("./_util.js");
const from_file_url_js_1 = require("./from_file_url.js");
/**
* Return the directory path of a `path`.
*
* @example Usage
* ```ts
* import { dirname } from "@std/path/posix/dirname";
* import { assertEquals } from "@std/assert";
*
* assertEquals(dirname("/home/user/Documents/"), "/home/user");
* assertEquals(dirname("/home/user/Documents/image.png"), "/home/user/Documents");
* assertEquals(dirname("https://deno.land/std/path/mod.ts"), "https://deno.land/std/path");
* assertEquals(dirname(new URL("file:///home/user/Documents/image.png")), "/home/user/Documents");
* ```
*
* @example Working with URLs
*
* ```ts
* import { dirname } from "@std/path/posix/dirname";
* import { assertEquals } from "@std/assert";
*
* assertEquals(dirname("https://deno.land/std/path/mod.ts"), "https://deno.land/std/path");
* assertEquals(dirname("https://deno.land/std/path/mod.ts?a=b"), "https://deno.land/std/path");
* assertEquals(dirname("https://deno.land/std/path/mod.ts#header"), "https://deno.land/std/path");
* ```
*
* @param path The path to get the directory from.
* @returns The directory path.
*/
function dirname(path) {
if (path instanceof URL) {
path = (0, from_file_url_js_1.fromFileUrl)(path);
}
(0, dirname_js_1.assertArg)(path);
let end = -1;
let matchedNonSeparator = false;
for (let i = path.length - 1; i >= 1; --i) {
if ((0, _util_js_1.isPosixPathSeparator)(path.charCodeAt(i))) {
if (matchedNonSeparator) {
end = i;
break;
}
}
else {
matchedNonSeparator = true;
}
}
// No matches. Fallback based on provided path:
//
// - leading slashes paths
// "/foo" => "/"
// "///foo" => "/"
// - no slash path
// "foo" => "."
if (end === -1) {
return (0, _util_js_1.isPosixPathSeparator)(path.charCodeAt(0)) ? "/" : ".";
}
return (0, strip_trailing_separators_js_1.stripTrailingSeparators)(path.slice(0, end), _util_js_1.isPosixPathSeparator);
}