MCPcopy
hub / github.com/graphif/project-graph / relativePathToAbsolutePath

Function relativePathToAbsolutePath

app/src/utils/pathString.tsx:217–253  ·  view source on GitHub ↗
(currentPath: string, relativePath: string)

Source from the content-addressed store, hash-verified

215 * @returns
216 */
217 export function relativePathToAbsolutePath(currentPath: string, relativePath: string): string {
218 const { drive, parts: currentParts } = splitCurrentPath(currentPath);
219 const relativeParts = splitRelativePath(relativePath);
220
221 // 如果当前路径是文件(有扩展名),则去掉文件名部分,只保留目录
222 const isFile = hasFileExtension(currentParts[currentParts.length - 1]);
223 const directoryParts = isFile ? currentParts.slice(0, -1) : [...currentParts];
224
225 const mergedParts = [...directoryParts];
226 for (const part of relativeParts) {
227 if (part === "..") {
228 if (mergedParts.length > 0) {
229 mergedParts.pop();
230 }
231 } else if (part !== "." && part !== "") {
232 mergedParts.push(part);
233 }
234 }
235
236 let absolutePath;
237 if (drive) {
238 absolutePath = `${drive}/`;
239 } else {
240 absolutePath = "/";
241 }
242 absolutePath += mergedParts.join("/");
243
244 // 处理根目录情况
245 if (mergedParts.length === 0) {
246 absolutePath = drive ? `${drive}/` : "/";
247 }
248
249 // 替换多个连续的斜杠为单个斜杠
250 absolutePath = absolutePath.replace(/\/+/g, "/");
251
252 return absolutePath;
253 }
254
255 function splitCurrentPath(path: string) {
256 path = path.replace(/\\/g, "/");

Callers

nothing calls this directly

Calls 6

splitCurrentPathFunction · 0.85
splitRelativePathFunction · 0.85
hasFileExtensionFunction · 0.85
joinMethod · 0.80
popMethod · 0.45
pushMethod · 0.45

Tested by

no test coverage detected