import { assertArg } from "../_common/normalize.ts";import { CHAR_COLON } from "../_common/constants.ts";import { normalizeString } from "../_common/normalize_string.ts";import { isPathSeparator, isWindowsDeviceRoot } from "./_util.ts";
export function normalize(path: string): string { assertArg(path);
const len = path.length; let rootEnd = 0; let device: string | undefined; let isAbsolute = false; const code = path.charCodeAt(0);
if (len > 1) { if (isPathSeparator(code)) {
isAbsolute = true;
if (isPathSeparator(path.charCodeAt(1))) { let j = 2; let last = j; for (; j < len; ++j) { if (isPathSeparator(path.charCodeAt(j))) break; } if (j < len && j !== last) { const firstPart = path.slice(last, j); last = j; for (; j < len; ++j) { if (!isPathSeparator(path.charCodeAt(j))) break; } if (j < len && j !== last) { last = j; for (; j < len; ++j) { if (isPathSeparator(path.charCodeAt(j))) break; } if (j === len) {
return `\\\\${firstPart}\\${path.slice(last)}\\`; } else if (j !== last) {
device = `\\\\${firstPart}\\${path.slice(last, j)}`; rootEnd = j; } } } } else { rootEnd = 1; } } else if (isWindowsDeviceRoot(code)) {
if (path.charCodeAt(1) === CHAR_COLON) { device = path.slice(0, 2); rootEnd = 2; if (len > 2) { if (isPathSeparator(path.charCodeAt(2))) { isAbsolute = true; rootEnd = 3; } } } } } else if (isPathSeparator(code)) { return "\\"; }
let tail: string; if (rootEnd < len) { tail = normalizeString( path.slice(rootEnd), !isAbsolute, "\\", isPathSeparator, ); } else { tail = ""; } if (tail.length === 0 && !isAbsolute) tail = "."; if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) { tail += "\\"; } if (device === undefined) { if (isAbsolute) { if (tail.length > 0) return `\\${tail}`; else return "\\"; } else if (tail.length > 0) { return tail; } else { return ""; } } else if (isAbsolute) { if (tail.length > 0) return `${device}\\${tail}`; else return `${device}\\`; } else if (tail.length > 0) { return device + tail; } else { return device; }}