I hope I understood your request correctly - what does it look like below? (You may need to configure baseURL if I fail a bit from this).
Function
function resolvePath (relativePath) { var protocol = "file:///c:/"; var baseUrl = "source/JasmineTest"; var reUpward = /\.\.\//g; var upwardCount = (relativePath.match(reUpward) || []).length; return protocol + (!upwardCount ? baseUrl : baseUrl.split("/").slice(0, -upwardCount).join("/")) + "/" + relativePath.replace(reUpward, ""); }
Call examples;
resolvePath("SpecRunner.html"); // "file:///c:/source/JasmineTest/SpecRunner.html" resolvePath("path/SpecRunner.html"); // "file:///c:/source/JasmineTest/path/SpecRunner.html" resolvePath("../../SpecRunner.html"); // "file:///c://SpecRunner.html" resolvePath("../SpecRunner.html"); // "file:///c:/source/SpecRunner.html" resolvePath("SpecRunner.html"); // "file:///c:/source/JasmineTest/SpecRunner.html"
Here is also a longer version, which should be easier to understand, it is the same as resolvePath;
function longerVersionOfResolvePath (relativePath) { var protocol = "file:///c:/"; var baseUrl = "source/JasmineTest"; var reUpward = /\.\.\//g; var upwardCount = (relativePath.match(reUpward) || []).length; var walkUpwards = upwardCount > 0; var relativePathWithUpwardStepsRemoved = relativePath.replace(reUpward, ""); var folderWalkedUpTo = baseUrl.split("/").slice(0, -upwardCount).join("/"); if (walkUpwards) { return protocol + folderWalkedUpTo + "/" + relativePathWithUpwardStepsRemoved; } else { return protocol + baseUrl + "/" + relativePath; } }
source share