1

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).

Basically, there is a virtual file system, that starts with, say, "C:/", and goes down like every other path system. 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.

The hard part to process is the ../../ part, so the expected output string from the above would be

"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. Here's my function, currently

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>

1 Answer 1

3

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);

Sign up to request clarification or add additional context in comments.

1 Comment

perfect, simple enough, it worked great, didn't think to jsut literally take off the last result of the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.