Created
May 8, 2020 21:18
-
-
Save haxiomic/98c61a09f6028fd72dccc0053385af76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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