Skip to content

Instantly share code, notes, and snippets.

@Elevista
Last active March 14, 2016 06:06
Show Gist options
  • Save Elevista/62f2ec6128acd02e152b to your computer and use it in GitHub Desktop.
Save Elevista/62f2ec6128acd02e152b to your computer and use it in GitHub Desktop.

##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);
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment