From 71c71ac890cc71dcc32c41b7a5280dbe13232f56 Mon Sep 17 00:00:00 2001 From: rswindell <> Date: Sun, 26 Apr 2020 04:41:58 +0000 Subject: [PATCH] A function (get) that returns the relative path between two absolute paths (directories, real or theoretical). --- exec/load/relpath.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 exec/load/relpath.js diff --git a/exec/load/relpath.js b/exec/load/relpath.js new file mode 100644 index 0000000000..d7c0503d3c --- /dev/null +++ b/exec/load/relpath.js @@ -0,0 +1,26 @@ +// $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; -- GitLab