Repository navigation
Expand file tree
/
Copy pathpath.ts
More file actions
149 lines (123 loc) · 4.79 KB
/
Copy pathpath.ts
File metadata and controls
149 lines (123 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
export function getFilename(path: string | undefined) {
if (!path) return ""
const trimmed = path.replace(/[/\\]+$/, "")
const index = Math.max(trimmed.lastIndexOf("/"), trimmed.lastIndexOf("\\"))
return trimmed.slice(index + 1)
}
export function getDirectory(path: string | undefined) {
if (!path) return ""
const trimmed = path.replace(/[/\\]+$/, "")
const parts = trimmed.split(/[/\\]/)
return parts.slice(0, parts.length - 1).join("/") + "/"
}
export function getFilenameTruncated(path: string | undefined, maxLength = 20) {
const filename = getFilename(path)
if (filename.length <= maxLength) return filename
const lastDot = filename.lastIndexOf(".")
const extension = lastDot <= 0 ? "" : filename.slice(lastDot)
const available = maxLength - extension.length - 1
if (available <= 0) return filename.slice(0, maxLength - 1) + "…"
return filename.slice(0, available) + "…" + extension
}
export function truncateMiddle(text: string, maxLength = 20) {
if (maxLength <= 0) return ""
if (text.length <= maxLength) return text
const available = maxLength - 1
const start = Math.ceil(available / 2)
const end = Math.floor(available / 2)
if (end <= 0) return text.slice(0, start) + "…"
return text.slice(0, start) + "…" + text.slice(-end)
}
const isDrive = (value: string) => {
if (value.length !== 2) return false
const code = value.charCodeAt(0)
return value[1] === ":" && ((code >= 65 && code <= 90) || (code >= 97 && code <= 122))
}
const trimTrailingSlashes = (value: string) => {
for (let i = value.length - 1; i >= 0; i--) {
if (value[i] !== "/") return value.slice(0, i + 1)
}
return ""
}
const isWindowsPath = (value: string) => value[1] === ":" || value.startsWith("\\\\")
/** A comparable form of a directory path: forward slashes for Windows paths, no trailing slash except on roots. */
export function comparablePath(path: string) {
const value = isWindowsPath(path) ? path.replaceAll("\\", "/") : path
const trimmed = trimTrailingSlashes(value)
if (!trimmed && value.startsWith("/")) return "/"
if (isDrive(trimmed)) return `${trimmed}/`
return trimmed
}
/** `child` is `parent` or inside it. Windows drive and UNC paths compare case-insensitively. */
export function containsDirectory(parent: string, child: string) {
const normalize = (value: string) => {
const key = comparablePath(value)
return /^[a-z]:\//i.test(key) || key.startsWith("//") ? key.toLowerCase() : key
}
const root = normalize(parent)
const target = normalize(child)
return target === root || target.startsWith(root.endsWith("/") ? root : `${root}/`)
}
export function sameDirectory(a: string, b: string) {
return containsDirectory(a, b) && containsDirectory(b, a)
}
export function encodeFilePath(filepath: string): string {
// Normalize Windows paths: convert backslashes to forward slashes
const normalized = filepath.replace(/\\/g, "/")
// Handle Windows absolute paths (D:/path -> /D:/path for proper file:// URLs)
const rooted = /^[A-Za-z]:/.test(normalized) ? "/" + normalized : normalized
// Encode each path segment (preserving forward slashes as path separators)
// Keep the colon in Windows drive letters (`/C:/...`) so downstream file URL parsers
// can reliably detect drives.
return rooted
.split("/")
.map((segment, index) => {
if (index === 1 && /^[A-Za-z]:$/.test(segment)) return segment
return encodeURIComponent(segment)
})
.join("/")
}
const MAX_LINE_NUMBER = 1_000_000
const lineHashPattern = /^#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i
const lineHashSuffixPattern = /^(.*?)#L(\d+)(?:C\d+)?(?:-L?(\d+)(?:C\d+)?)?$/i
const lineColonSuffixPattern = /^(.*?):(\d+)(?::\d+)?(?:-(\d+)(?::\d+)?)?:?$/
function toLineRange(first: number, second: number) {
if (
!Number.isSafeInteger(first) ||
!Number.isSafeInteger(second) ||
first < 1 ||
second < 1 ||
first > MAX_LINE_NUMBER ||
second > MAX_LINE_NUMBER
) {
return undefined
}
return {
start: Math.min(first, second),
end: Math.max(first, second),
}
}
export function isLineRangeHash(hash: string) {
return lineHashPattern.test(hash)
}
export function parsePathLineSuffix(input: string) {
const hashMatch = input.match(lineHashSuffixPattern)
if (hashMatch) {
const first = Number(hashMatch[2])
const second = hashMatch[3] ? Number(hashMatch[3]) : first
return {
path: hashMatch[1] ?? "",
selection: toLineRange(first, second),
}
}
const colonMatch = input.match(lineColonSuffixPattern)
if (colonMatch && !/^[a-z]:?$/i.test(colonMatch[1] ?? "")) {
const first = Number(colonMatch[2])
const second = colonMatch[3] ? Number(colonMatch[3]) : first
return {
path: colonMatch[1] ?? "",
selection: toLineRange(first, second),
}
}
return { path: input, selection: undefined }
}