Skip to content

Instantly share code, notes, and snippets.

@jason-fonseca
Created April 2, 2012 02:22
Show Gist options
  • Save jason-fonseca/2280100 to your computer and use it in GitHub Desktop.
Save jason-fonseca/2280100 to your computer and use it in GitHub Desktop.
pathjoin.js
function Pathify(str)
{
if(arguments.length == 2 && arguments[1] instanceof Array)
{
var arr = arguments[1];
for(var i = 0; i < arr.length; i++)
{
if(arr[i] instanceof Array)
{
str = Pathify(str, arr[i]);
}
else
{
str += "/" + arr[i] + "/";
}
}
}
else
{
for(var i = 1; i < arguments.length; i++)
{
if(arguments[i] instanceof Array)
{
str = Pathify(str, arguments[i]);
}
else
{
str += "/" + arguments[i] + "/";
}
}
}
//if(typeof(str)!="string") str="";
var Proto = str.match(/^([^:]+):\/\/(.*)/);
if(Proto)
str = Proto[2];
var PathSegments = str.split(/\/+/g);
for(var i = 1; i < PathSegments.length; i++)
{
if(/^\.\.$/.test(PathSegments[i]))
{
PathSegments.splice(i - 1, 2);
i--;
}
else if(/^\.$/.test(PathSegments[i]))
{
PathSegments.splice(i, 1);
i--;
}
}
return (Proto ? Proto[1] + "://" : "") + PathSegments.join("/");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment