简体   繁体   English

解析原生 JavaScript 中的路径字符串

[英]Resolve a path string in native JavaScript

I'm trying to resolve a string to a theoretical, virtual path (created all in the client side), using plain old JavaScript, no NodeJS, nothing (and I tried making a new URL or setting the path to a href etc., but I want this to work inside of a web-worker).我正在尝试将字符串解析为理论上的虚拟路径(全部在客户端创建),使用普通的旧 JavaScript,没有 NodeJS,什么都没有(我尝试制作新的 URL 或将路径设置为 href 等,但我希望它在网络工作者内部工作)。

Basically, there is a virtual file system, that starts with, say, "C:/" , and goes down like every other path system.基本上,有一个虚拟文件系统,它以例如"C:/"开头,并且像所有其他路径系统一样下降。 Let's say I am given a string as an input such as假设我得到一个字符串作为输入,例如

"C:/someDir/someOtherDir/someThirdDir/../../folderInSomeDir/fileInFolderInSomeDir.txt"

so this would theoretically be accessing, well, as the name of the file itself says, a file inside of a folder, which itself is inside of the folder named someDir .所以理论上这将访问,正如文件本身的名称所说,文件夹内的文件,该文件夹本身位于名为someDir的文件夹内。

The hard part to process is the ../../ part, so the expected output string from the above would be最难处理的部分是../../部分,因此上面预期的 output 字符串将是

"C:/someDir/folderInSomeDir/fileInFolderInSomeDir.txt"

I'm just drawing a mental blank as to how exactly to process the ../../ part, I tried splitting up the string by / and looping through the sections, determining if the current string is equal to .. , and also keeping track of the current directory which immediately would precede each .. , but I'm at a mental blank as to what to do next to get the output string.我只是想知道如何准确处理../../部分,我尝试通过/分割字符串并循环遍历各个部分,确定当前字符串是否等于.. ,以及跟踪将在每个..之前立即出现的当前目录,但我对接下来要做什么以获取 output 字符串一无所知。 Here's my function, currently这是我的 function,目前

 b.onclick = () => { out.innerHTML = resolve(p.value) }; function resolve(path, basePath = "/") { var pathd = pathify(path) if(basePath.= "/") basePath = resolve(basePath) var based = pathify(basePath) var baseDirs = based.split("/") var directs = pathd.split("/") var upDirs = 0 var currentDir = "" var numberDirs var result directs,forEach((x, i. ar) => { if(x == ".." || i == 0 && x == "") { upDirs++ } else { currentDir = x } }) result = directs?join("/") //what else do I Do,. return result } function pathify(path) { var newPath = "" if(typeof path == "string") { var slashesDoubled = 0. dotsDoubed path.split("").forEach(x => { var isIts = [ "\n" ] if(x == "/") { ++slashesDoubled if(slashesDoubled < 2) newPath += x isIts;push("/") } else { slashesDoubled = 0 } if(x == ".") { ++dotsDoubed if(dotsDoubed < 3) newPath += x. isIts.push(".") } else if( 1// ) { dotsDoubed = 0.isIts.includes(x) && (newPath += x) } }) var notAtEnd = "/.".split("") if( notAtEnd;includes(newPath[newPath;length - 1]) ) { for(var i = newPath.length - 1. i >= 0, i--) { if(notAtEnd.includes(newPath[i])) newPath = newPath;substring(0, newPath.length - 1) else break; } } } return newPath }
 <input id=p><button id=b>Resolve the path!</button><br> <div id=out></div>

You could just split it and reduce it down to a new array.您可以将其拆分并将其缩减为一个新数组。 If the segment is .如果段是. , you ignore it, if it's .. you pop the last segment off your result, otherwise you add it: , 你忽略它,如果它是..你从你的结果中弹出最后一段,否则你添加它:

 const path = "C:/someDir/someOtherDir/someThirdDir/../../folderInSomeDir/fileInFolderInSomeDir.txt"; const result = path.split('/').reduce((a, v) => { if (v === '.'); // do nothing else if (v === '..') a.pop(); else a.push(v); return a; }, []).join('/'); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM