MCPcopy
hub / github.com/coder/mux / parse

Method parse

src/node/utils/paths.main.ts:68–128  ·  view source on GitHub ↗

* Split path into components (OS-aware)

(filePath: string)

Source from the content-addressed store, hash-verified

66 * Split path into components (OS-aware)
67 */
68 static parse(filePath: string): PathComponents {
69 if (!filePath || typeof filePath !== "string") {
70 return { root: "", segments: [], basename: filePath };
71 }
72
73 const original = filePath;
74 let root = "";
75 let dir = "";
76 let base = "";
77
78 // Determine basename and directory
79 const lastSlash = isWindowsPlatform()
80 ? Math.max(original.lastIndexOf("/"), original.lastIndexOf("\\"))
81 : original.lastIndexOf("/");
82 if (lastSlash === -1) {
83 base = original;
84 dir = "";
85 } else {
86 base = original.slice(lastSlash + 1);
87 dir = original.slice(0, lastSlash);
88 }
89
90 // Determine root
91 if (isWindowsPlatform()) {
92 const driveMatch = /^[A-Za-z]:[\\/]/.exec(original);
93 if (driveMatch) {
94 root = driveMatch[0];
95 // Ensure dir does not include root
96 if (dir.startsWith(root)) {
97 dir = dir.slice(root.length);
98 }
99 } else if (original.startsWith("\\\\")) {
100 // UNC paths - treat leading double-backslash as root
101 root = "\\\\";
102 if (dir.startsWith(root)) {
103 dir = dir.slice(root.length);
104 }
105 }
106 // Also treat Unix-style absolute paths as absolute even on Windows
107 if (!root && original.startsWith("/")) {
108 root = "/";
109 if (dir.startsWith(root)) {
110 dir = dir.slice(root.length);
111 }
112 }
113 } else if (original.startsWith("/")) {
114 root = "/";
115 if (dir.startsWith(root)) {
116 dir = dir.slice(root.length);
117 }
118 }
119
120 const separatorRegex = isWindowsPlatform() ? /[\\/]+/ : /\/+/;
121 const segments = dir ? dir.split(separatorRegex).filter(Boolean) : [];
122
123 return {
124 root,
125 segments,

Callers 15

abbreviateMethod · 0.95
loadConfigOrDefaultMethod · 0.45
findWorkspaceMethod · 0.45
loadProvidersConfigMethod · 0.45
loadSecretsConfigMethod · 0.45
config.test.tsFile · 0.45
readRawArchiveConfigFunction · 0.45
readRawConfigFunction · 0.45
paths.test.tsFile · 0.45
parseJwtClaimsFunction · 0.45
eventStore.test.tsFile · 0.45

Calls 2

isWindowsPlatformFunction · 0.70
execMethod · 0.65

Tested by 2

readRawArchiveConfigFunction · 0.36
readRawConfigFunction · 0.36