Skip to content

Instantly share code, notes, and snippets.

@haxiomic
Created May 8, 2020 21:18
Show Gist options
  • Save haxiomic/98c61a09f6028fd72dccc0053385af76 to your computer and use it in GitHub Desktop.
Save haxiomic/98c61a09f6028fd72dccc0053385af76 to your computer and use it in GitHub Desktop.
static function relativePath(relativeTo: String, path: String) {
// make both absolute
path = Path.removeTrailingSlashes(FileSystem.absolutePath(path));
relativeTo = Path.removeTrailingSlashes(FileSystem.absolutePath(relativeTo));
var aPath = path.split('/');
var aRelativeTo = relativeTo.split('/');
// find shared part of path
var matchesUpToIndex = 0;
for (i in 0...aRelativeTo.length) {
if (aPath[i] == aRelativeTo[i]) {
matchesUpToIndex = i;
} else {
break;
}
}
return [for (_ in 0...(aRelativeTo.length - 1) - matchesUpToIndex) '..']
.concat(aPath.slice(matchesUpToIndex + 1))
.join('/');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment