-
-
Save chrisevans/1127011 to your computer and use it in GitHub Desktop.
Relative path resolver
This file contains 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
function ( | |
c, // the current path in the form "a/b/c/" | |
p, // a relative path in the form "./d/../e/f" | |
d // (placeholder) | |
) { | |
c = c.split("/"); // convert to parts | |
p = p.split("/"); // convert to parts | |
c.pop(); // drop "document" part of current path | |
while (d = p.shift()) // grab next part from beginning of relative path | |
d == ".." && // if parent folder reference | |
c.pop() // then drop the last part from current path | |
|| d != "." && // else if not current folder reference | |
c.push(d); // then append to current path | |
return c.join("/") // convert to a path | |
} |
This file contains 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
function(c,p,d){c=c.split("/");p=p.split("/");c.pop();while(d=p.shift())d==".."&&c.pop()||d!="."&&c.push(d);return c.join("/")} |
This file contains 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
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Ates Goral <http://magnetiq.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains 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
{ | |
"name": "resolvePath", | |
"description": "Applies a relative path to the given path", | |
"keywords": [ | |
"path", | |
"relative", | |
"url", | |
"href" | |
] | |
} |
This file contains 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
<!DOCTYPE html> | |
<title>resolvePath</title> | |
<div>Expected value: <b>a/b/e/f</b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var resolvePath = function(c,p,d){c=c.split("/");p=p.split("/");c.pop();while(d=p.shift())d==".."&&c.pop()||d!="."&&c.push(d);return c.join("/")}; | |
document.getElementById( "ret" ).innerHTML = resolvePath("a/b/c", "./d/../e/f"); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment