/** * Path utilities for virtual file systems */ /** * Normalize a path */ export function normalizePath(path: string): string { // Handle empty path as root if (!path) return "/"; // Ensure path starts with / if (!path.startsWith("/")) { path = "/" + path; } // Remove duplicate slashes path = path.replace(/\/+/g, "/"); // Remove trailing slash except for root if (path.length > 1 && path.endsWith("/")) { path = path.slice(0, -1); } return path; } /** * Get directory name from path */ export function dirname(path: string): string { const normalized = normalizePath(path); const lastSlash = normalized.lastIndexOf("/"); return lastSlash === 0 ? "/" : normalized.slice(0, lastSlash); } /** * Get base name from path */ export function basename(path: string): string { const normalized = normalizePath(path); const lastSlash = normalized.lastIndexOf("/"); return normalized.slice(lastSlash + 1); } /** * Join path segments */ export function join(...segments: string[]): string { const joined = segments.join("/"); return normalizePath(joined); } /** * Check if path is absolute */ export function isAbsolute(path: string): boolean { return path.startsWith("/"); } /** * Get file extension */ export function extname(path: string): string { const base = basename(path); const lastDot = base.lastIndexOf("."); return lastDot === -1 ? "" : base.slice(lastDot); }