Files
t3code/packages/shared/src/path.ts
T
2026-10-01 00:15:28 -07:00

77 lines
2.4 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;
}
// Windows refuses these as file names, with or without an extension.
const WINDOWS_RESERVED_NAME = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/;
/**
* Folder name for a project started from just a name ("Pinball Stats" becomes
* "pinball-stats"). The server uses it for `projects.createNew`, and clients
* use it to show the path before the server makes it.
*/
export function newProjectFolderName(name: string): string {
const slug = name
.normalize("NFKD")
.replace(/[̀-ͯ]/g, "")
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+/, "")
.slice(0, 64)
.replace(/-+$/, "");
if (slug.length === 0) return "project";
return WINDOWS_RESERVED_NAME.test(slug) ? `${slug}-project` : slug;
}