Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
56 lines
1.7 KiB
TypeScript
56 lines
1.7 KiB
TypeScript
export function isWindowsDrivePath(value: string): boolean {
|
|
return /^[a-zA-Z]:([/\\]|$)/.test(value);
|
|
}
|
|
|
|
export function isUncPath(value: string): boolean {
|
|
return value.startsWith("\\\\");
|
|
}
|
|
|
|
export function isWindowsAbsolutePath(value: string): boolean {
|
|
return isUncPath(value) || isWindowsDrivePath(value);
|
|
}
|
|
|
|
export function isExplicitRelativePath(value: string): boolean {
|
|
return (
|
|
value === "." ||
|
|
value === ".." ||
|
|
value.startsWith("./") ||
|
|
value.startsWith("../") ||
|
|
value.startsWith(".\\") ||
|
|
value.startsWith("..\\")
|
|
);
|
|
}
|
|
|
|
function isRootPath(value: string): boolean {
|
|
// The drive separator is required: a bare `C:` is not the drive root (it
|
|
// means "current directory on C:"), and treating it as already-canonical
|
|
// would leave it as `C:` while `C:\` and `C:/` normalize to the drive root,
|
|
// so the same location would fail project identity/dedup comparisons.
|
|
return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]$/.test(value);
|
|
}
|
|
|
|
function trimTrailingPathSeparators(value: string): string {
|
|
if (value.length === 0 || isRootPath(value)) {
|
|
return value;
|
|
}
|
|
const trimmed = value.startsWith("/")
|
|
? value.replace(/\/+$/g, "")
|
|
: value.replace(/[\\/]+$/g, "");
|
|
if (trimmed.length === 0) {
|
|
return value;
|
|
}
|
|
return /^[a-zA-Z]:$/.test(trimmed) ? `${trimmed}\\` : trimmed;
|
|
}
|
|
|
|
export function normalizeProjectPathForDispatch(value: string): string {
|
|
return trimTrailingPathSeparators(value.trim());
|
|
}
|
|
|
|
export function normalizeProjectPathForComparison(value: string): string {
|
|
const normalized = normalizeProjectPathForDispatch(value);
|
|
if (isWindowsDrivePath(normalized) || isUncPath(normalized)) {
|
|
return normalized.replaceAll("/", "\\").toLowerCase();
|
|
}
|
|
return normalized;
|
|
}
|