Skip to content

Instantly share code, notes, and snippets.

@atesgoral
Forked from 140bytes/LICENSE.txt
Created June 2, 2011 20:36
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save atesgoral/1005248 to your computer and use it in GitHub Desktop.
Save atesgoral/1005248 to your computer and use it in GitHub Desktop.
Relative path resolver
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
}
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("/")}
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.
{
"name": "resolvePath",
"description": "Applies a relative path to the given path",
"keywords": [
"path",
"relative",
"url",
"href"
]
}
<!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