##Example
var p = new path('http://abc.com/aa/bb/cc');
p.cd('../../ww');
p.toString();
//returns "http://abc.com/aa/ww"
p.cd('/').toString();
//returns "http://abc.com"
;(function () { | |
this.path = function (dir) { | |
var temp = dir.split(/(\w+\:\/\/[\w.]+)/); | |
var root = ''; | |
if (!temp[0] && !temp.shift()) | |
root = temp.shift(); | |
var _path = temp.pop().split('/'); | |
if (_path[0] === '') | |
_path.shift(); | |
this.upDir = function () { | |
_path.pop(); | |
return this; | |
}; | |
this.toString = function () { | |
if (_path.length) | |
return root + '/' + _path.join('/'); | |
else | |
return root || '/'; | |
}; | |
var self = this; | |
function _cd(param) { | |
var head = param.shift(); | |
if (head === '..') | |
_path.pop(); | |
else if (head !== '.' && head !== '') | |
_path.push(head); | |
if (param.length) | |
return _cd(param); | |
else | |
return self; | |
} | |
this.cd = function (str) { | |
if (!str) | |
return this; | |
var param = str.split('/'); | |
if (param[0] === '') { | |
param.shift(); | |
_path = []; | |
} | |
return _cd(param); | |
}; | |
} | |
})(); |