Skip to content
Snippets Groups Projects
Commit 71c71ac8 authored by rswindell's avatar rswindell
Browse files

A function (get) that returns the relative path between two absolute paths

(directories, real or theoretical).
parent 784ea14f
Branches
Tags
No related merge requests found
// $Id$
// Given 2 absolute directories: a working directory (cwd) and a target (dir),
// return the relative path to reach the target directory.
// The given directories do not have to actually exist.
// Returns a slash-separated path, terminated with a trailing slash.
function get(cwd, dir)
{
var cwd = backslash(fullpath(cwd)).replace('\\', '/', 'g').split('/');
var dir = backslash(fullpath(dir)).replace('\\', '/', 'g').split('/');
var common;
for(common = 0; common < cwd.length; common++) {
if(cwd[common] != dir[common])
break;
}
var result = [];
if(common) {
cwd.splice(0, common);
dir.splice(0, common);
while(result.length < cwd.length - 1)
result.push("..");
}
return result.concat(dir).join('/');
}
this;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment