UNPKG

yahoo-finance2

Version:
97 lines (96 loc) 3.77 kB
"use strict"; // Copyright 2018-2025 the Deno authors. MIT license. // This module is browser compatible. Object.defineProperty(exports, "__esModule", { value: true }); exports.extname = extname; const constants_js_1 = require("../_common/constants.js"); const assert_path_js_1 = require("../_common/assert_path.js"); const _util_js_1 = require("./_util.js"); const from_file_url_js_1 = require("./from_file_url.js"); /** * Return the extension of the `path` with leading period. * * @example Usage * ```ts * import { extname } from "@std/path/posix/extname"; * import { assertEquals } from "@std/assert"; * * assertEquals(extname("/home/user/Documents/file.ts"), ".ts"); * assertEquals(extname("/home/user/Documents/"), ""); * assertEquals(extname("/home/user/Documents/image.png"), ".png"); * assertEquals(extname(new URL("file:///home/user/Documents/file.ts")), ".ts"); * assertEquals(extname(new URL("file:///home/user/Documents/file.ts?a=b")), ".ts"); * assertEquals(extname(new URL("file:///home/user/Documents/file.ts#header")), ".ts"); * ``` * * @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 { extname } from "@std/path/posix/extname"; * import { assertEquals } from "@std/assert"; * * assertEquals(extname("https://deno.land/std/path/mod.ts"), ".ts"); * assertEquals(extname("https://deno.land/std/path/mod.ts?a=b"), ".ts?a=b"); * assertEquals(extname("https://deno.land/std/path/mod.ts#header"), ".ts#header"); * ``` * * @param path The path to get the extension from. * @returns The extension (ex. for `file.ts` returns `.ts`). */ function extname(path) { if (path instanceof URL) { path = (0, from_file_url_js_1.fromFileUrl)(path); } (0, assert_path_js_1.assertPath)(path); let startDot = -1; let startPart = 0; let end = -1; let matchedSlash = true; // Track the state of characters (if any) we see before our first dot and // after any path separator we find let preDotState = 0; for (let i = path.length - 1; i >= 0; --i) { const code = path.charCodeAt(i); if ((0, _util_js_1.isPosixPathSeparator)(code)) { // If we reached a path separator that was not part of a set of path // separators at the end of the string, stop now if (!matchedSlash) { startPart = i + 1; break; } continue; } if (end === -1) { // We saw the first non-path separator, mark this as the end of our // extension matchedSlash = false; end = i + 1; } if (code === constants_js_1.CHAR_DOT) { // If this is our first dot, mark it as the start of our extension if (startDot === -1) startDot = i; else if (preDotState !== 1) preDotState = 1; } else if (startDot !== -1) { // We saw a non-dot and non-path separator before our dot, so we should // have a good chance at having a non-empty extension preDotState = -1; } } if (startDot === -1 || end === -1 || // We saw a non-dot character immediately before the dot preDotState === 0 || // The (right-most) trimmed path component is exactly '..' (preDotState === 1 && startDot === end - 1 && startDot === startPart + 1)) { return ""; } return path.slice(startDot, end); }