deno.land / x / lume@v2.1.4 / core / utils / path.ts
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101import { posix, SEPARATOR } from "../../deps/path.ts";
/** * Convert the Windows paths (that use the separator "\") * to Posix paths (with the separator "/") * and ensure it starts with "/". */export function normalizePath(path: string, rootToRemove?: string) { if (rootToRemove) { path = path.replace(rootToRemove, ""); }
if (SEPARATOR !== "/") { path = path.replaceAll(SEPARATOR, "/");
// Is absolute Windows path (C:/...) if (path.includes(":/")) { if (rootToRemove && path.startsWith(rootToRemove)) { return posix.join("/", path.replace(rootToRemove, "")); }
return path; } }
const absolute = posix.join("/", path); return rootToRemove && absolute.startsWith(rootToRemove) ? posix.join("/", absolute.replace(rootToRemove, "")) : absolute;}
/** Check if the path is an URL */export function isUrl(path: string): boolean { return !!path.match(/^(https?|file):\/\//);}
/** Check if the path is absolute */export function isAbsolutePath(path: string): boolean { return SEPARATOR !== "/" ? /^\w:[\/\\]/.test(path) : path.startsWith("/");}
/** Replace the extension of a path */export function replaceExtension( path: string, ext: string,): string { return path.replace(/\.\w+$/, ext);}
/** Split a path to path + extension */export function getPathAndExtension(path: string): [string, string] { const extension = getExtension(path); const pathWithoutExtension = path.slice(0, -extension.length); return [pathWithoutExtension, extension];}
/** Get the extension of a path (this works better than std/path) */export function getExtension(path: string): string { const match = path.match(/\.\w+$/); return match ? match[0] : "";}
export type Extensions = string[] | "*";
export function matchExtension(exts: Extensions, path: string): boolean { if (exts === "*") { return true; }
return exts.some((ext) => path.endsWith(ext));}
/** * Resolve the path of an included file * Used in the template engines and processors */export function resolveInclude( path: string, includesDir: string, fromDir?: string, rootToRemove?: string,): string { if (isUrl(path)) { return path; }
if (path.startsWith(".")) { if (!fromDir) { throw new Error(`Cannot load "${path}" without a base directory`); }
return normalizePath(posix.join(fromDir, path), rootToRemove); }
const normalized = normalizePath(path, rootToRemove);
return normalized.startsWith(normalizePath(posix.join(includesDir, "/"))) ? normalized : normalizePath(posix.join(includesDir, normalized));}
Version Info