import { assert } from "../assert/assert.ts";import { assertPath, isPathSeparator } from "./_util.ts";import { posixNormalize, windowsNormalize } from "./_normalize.ts";
export function posixJoin(...paths: string[]): string { if (paths.length === 0) return ".";
let joined: string | undefined; for (let i = 0, len = paths.length; i < len; ++i) { const path = paths[i]; assertPath(path); if (path.length > 0) { if (!joined) joined = path; else joined += `/${path}`; } } if (!joined) return "."; return posixNormalize(joined);}
export function windowsJoin(...paths: string[]): string { if (paths.length === 0) return ".";
let joined: string | undefined; let firstPart: string | null = null; for (let i = 0; i < paths.length; ++i) { const path = paths[i]; assertPath(path); if (path.length > 0) { if (joined === undefined) joined = firstPart = path; else joined += `\\${path}`; } }
if (joined === undefined) return ".";
let needsReplace = true; let slashCount = 0; assert(firstPart !== null); if (isPathSeparator(firstPart.charCodeAt(0))) { ++slashCount; const firstLen = firstPart.length; if (firstLen > 1) { if (isPathSeparator(firstPart.charCodeAt(1))) { ++slashCount; if (firstLen > 2) { if (isPathSeparator(firstPart.charCodeAt(2))) ++slashCount; else { needsReplace = false; } } } } } if (needsReplace) { for (; slashCount < joined.length; ++slashCount) { if (!isPathSeparator(joined.charCodeAt(slashCount))) break; }
if (slashCount >= 2) joined = `\\${joined.slice(slashCount)}`; }
return windowsNormalize(joined);}