UNPKG

yahoo-finance2

Version:
139 lines (138 loc) 5.19 kB
"use strict"; // Copyright 2018-2025 the Deno authors. MIT license. // This module is browser compatible. Object.defineProperty(exports, "__esModule", { value: true }); exports.normalize = normalize; const normalize_js_1 = require("../_common/normalize.js"); const constants_js_1 = require("../_common/constants.js"); const normalize_string_js_1 = require("../_common/normalize_string.js"); const _util_js_1 = require("./_util.js"); const from_file_url_js_1 = require("./from_file_url.js"); /** * Normalize the `path`, resolving `'..'` and `'.'` segments. * Note that resolving these segments does not necessarily mean that all will be eliminated. * A `'..'` at the top-level will be preserved, and an empty path is canonically `'.'`. * * @example Usage * ```ts * import { normalize } from "@std/path/windows/normalize"; * import { assertEquals } from "@std/assert"; * * assertEquals(normalize("C:\\foo\\..\\bar"), "C:\\bar"); * assertEquals(normalize(new URL("file:///C:/foo/../bar")), "C:\\bar"); * ``` * * @param path The path to normalize * @returns The normalized path */ function normalize(path) { if (path instanceof URL) { path = (0, from_file_url_js_1.fromFileUrl)(path); } (0, normalize_js_1.assertArg)(path); const len = path.length; let rootEnd = 0; let device; let isAbsolute = false; const code = path.charCodeAt(0); // Try to match a root if (len > 1) { if ((0, _util_js_1.isPathSeparator)(code)) { // Possible UNC root // If we started with a separator, we know we at least have an absolute // path of some kind (UNC or otherwise) isAbsolute = true; if ((0, _util_js_1.isPathSeparator)(path.charCodeAt(1))) { // Matched double path separator at beginning let j = 2; let last = j; // Match 1 or more non-path separators for (; j < len; ++j) { if ((0, _util_js_1.isPathSeparator)(path.charCodeAt(j))) break; } if (j < len && j !== last) { const firstPart = path.slice(last, j); // Matched! last = j; // Match 1 or more path separators for (; j < len; ++j) { if (!(0, _util_js_1.isPathSeparator)(path.charCodeAt(j))) break; } if (j < len && j !== last) { // Matched! last = j; // Match 1 or more non-path separators for (; j < len; ++j) { if ((0, _util_js_1.isPathSeparator)(path.charCodeAt(j))) break; } if (j === len) { // We matched a UNC root only // Return the normalized version of the UNC root since there // is nothing left to process return `\\\\${firstPart}\\${path.slice(last)}\\`; } else if (j !== last) { // We matched a UNC root with leftovers device = `\\\\${firstPart}\\${path.slice(last, j)}`; rootEnd = j; } } } } else { rootEnd = 1; } } else if ((0, _util_js_1.isWindowsDeviceRoot)(code)) { // Possible device root if (path.charCodeAt(1) === constants_js_1.CHAR_COLON) { device = path.slice(0, 2); rootEnd = 2; if (len > 2) { if ((0, _util_js_1.isPathSeparator)(path.charCodeAt(2))) { // Treat separator following drive name as an absolute path // indicator isAbsolute = true; rootEnd = 3; } } } } } else if ((0, _util_js_1.isPathSeparator)(code)) { // `path` contains just a path separator, exit early to avoid unnecessary // work return "\\"; } let tail; if (rootEnd < len) { tail = (0, normalize_string_js_1.normalizeString)(path.slice(rootEnd), !isAbsolute, "\\", _util_js_1.isPathSeparator); } else { tail = ""; } if (tail.length === 0 && !isAbsolute) tail = "."; if (tail.length > 0 && (0, _util_js_1.isPathSeparator)(path.charCodeAt(len - 1))) { tail += "\\"; } if (device === undefined) { if (isAbsolute) { if (tail.length > 0) return `\\${tail}`; else return "\\"; } return tail; } else if (isAbsolute) { if (tail.length > 0) return `${device}\\${tail}`; else return `${device}\\`; } return device + tail; }