Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

path.ts

55 lines1.7 KB

1export function isWindowsDrivePath(value: string): boolean {
2 return /^[a-zA-Z]:([/\\]|$)/.test(value);
3}
4​
5export function isUncPath(value: string): boolean {
6 return value.startsWith("\\\\");
7}
8​
9export function isWindowsAbsolutePath(value: string): boolean {
10 return isUncPath(value) || isWindowsDrivePath(value);
12​
13export function isExplicitRelativePath(value: string): boolean {
14 return (
15 value === "." ||
16 value === ".." ||
17 value.startsWith("./") ||
18 value.startsWith("../") ||
19 value.startsWith(".\\") ||
20 value.startsWith("..\\")
21 );
23​
24function isRootPath(value: string): boolean {
25 // The drive separator is required: a bare `C:` is not the drive root (it
26 // means "current directory on C:"), and treating it as already-canonical
27 // would leave it as `C:` while `C:\` and `C:/` normalize to the drive root,
28 // so the same location would fail project identity/dedup comparisons.
29 return value === "/" || value === "\\" || /^[a-zA-Z]:[/\\]$/.test(value);
31​
32function trimTrailingPathSeparators(value: string): string {
33 if (value.length === 0 || isRootPath(value)) {
34 return value;
35 }
36 const trimmed = value.startsWith("/")
37 ? value.replace(/\/+$/g, "")
38 : value.replace(/[\\/]+$/g, "");
39 if (trimmed.length === 0) {
40 return value;
41 }
42 return /^[a-zA-Z]:$/.test(trimmed) ? `${trimmed}\\` : trimmed;
44​
45export function normalizeProjectPathForDispatch(value: string): string {
46 return trimTrailingPathSeparators(value.trim());
48​
49export function normalizeProjectPathForComparison(value: string): string {
50 const normalized = normalizeProjectPathForDispatch(value);
51 if (isWindowsDrivePath(normalized) || isUncPath(normalized)) {
52 return normalized.replaceAll("/", "\\").toLowerCase();
53 }
54 return normalized;