Skip to content

Commit

Permalink
url-decode the output of child(). version bump 0.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
cainus committed May 25, 2014
1 parent 424311a commit 1a2b51f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 31 deletions.
41 changes: 26 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,7 @@ UrlGrey.prototype.encode = function(str){
};

UrlGrey.prototype.decode = function(str){
try {
return decodeURIComponent(str);
} catch (ex) {
return querystring.unescape(str);
}
return decode(str);
};

UrlGrey.prototype.parent = function(){
Expand Down Expand Up @@ -311,17 +307,17 @@ UrlGrey.prototype.child = function(suffixes){
suffixes = argsArray(arguments);
if (suffixes.length > 0){
return this.query(false).hash('').path(this.path(), suffixes);
} else {
// if no suffix, return the child
var pieces = this.path().split("/");
var last = arrLast(pieces);
if ((pieces.length > 1) && (last === '')){
// ignore trailing slashes
pieces.pop();
last = arrLast(pieces);
}
return last;
}

// if no suffix, return the child
var pieces = pathPieces(this.path());
var last = arrLast(pieces);
if ((pieces.length > 1) && (last === '')){
// ignore trailing slashes
pieces.pop();
last = arrLast(pieces);
}
return last;
};

UrlGrey.prototype.toJSON = function(){
Expand Down Expand Up @@ -353,6 +349,21 @@ UrlGrey.prototype.toString = function(){
return retval;
};

var pathPieces = function(path){
var pieces = path.split('/');
for(var i = 0; i < pieces.length; i++){
pieces[i] = decode(pieces[i]);
}
return pieces;
};

var decode = function(str){
try {
return decodeURIComponent(str);
} catch (ex) {
return querystring.unescape(str);
}
};

var portString = function(o){
if (o.protocol() === 'https'){
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url",
"uri"
],
"version": "0.3.3",
"version": "0.4.0",
"bugs": {
"url": "https://github.com/cainus/urlgrey/issues"
},
Expand All @@ -20,7 +20,7 @@
"Gregg Caines <gregg@caines.ca> (http://caines.ca)"
],
"dependencies": {
"tape": "~2.3.0"
"tape": "2.3.0"
},
"devDependencies": {
"jshint": "2.3.0",
Expand Down
8 changes: 8 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,14 @@ describe("urlgrey", function(){
var url = "http://asdf.com/path/kid/?asdf=1234#frag";
urlgrey(url).child().should.equal('kid');
});
it("url-decodes the child if it's encoded", function(){
var url = "http://asdf.com/path/the%20kid";
urlgrey(url).child().should.equal('the kid');
});
it("url-encodes the child if necessary", function(){
var url = "http://asdf.com/path/";
urlgrey(url).child('the kid').toString().should.equal('http://asdf.com/path/the%20kid');
});
});
describe("#parsed", function(){
it("returns some stuff", function(){
Expand Down
45 changes: 32 additions & 13 deletions urlgrey.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,7 +813,7 @@ UrlGrey.prototype.path = function(){
str = str.replace(/\/$/, ''); // remove all trailing slashes
args = str.split('/');
for(var i = 0; i < args.length; i++){
args[i] = encodeURIComponent(args[i]);
args[i] = this.encode(args[i]);
}
str = args.join('/');
if (str[0] !== '/'){ str = '/' + str; }
Expand All @@ -838,11 +838,15 @@ UrlGrey.prototype.rawPath = function(){
};

UrlGrey.prototype.encode = function(str){
return encodeURIComponent(str);
try {
return encodeURIComponent(str);
} catch (ex) {
return querystring.escape(str);
}
};

UrlGrey.prototype.decode = function(str){
return decodeURIComponent(str);
return decode(str);
};

UrlGrey.prototype.parent = function(){
Expand Down Expand Up @@ -879,17 +883,17 @@ UrlGrey.prototype.child = function(suffixes){
suffixes = argsArray(arguments);
if (suffixes.length > 0){
return this.query(false).hash('').path(this.path(), suffixes);
} else {
// if no suffix, return the child
var pieces = this.path().split("/");
var last = arrLast(pieces);
if ((pieces.length > 1) && (last === '')){
// ignore trailing slashes
pieces.pop();
last = arrLast(pieces);
}
return last;
}

// if no suffix, return the child
var pieces = pathPieces(this.path());
var last = arrLast(pieces);
if ((pieces.length > 1) && (last === '')){
// ignore trailing slashes
pieces.pop();
last = arrLast(pieces);
}
return last;
};

UrlGrey.prototype.toJSON = function(){
Expand Down Expand Up @@ -921,6 +925,21 @@ UrlGrey.prototype.toString = function(){
return retval;
};

var pathPieces = function(path){
var pieces = path.split('/');
for(var i = 0; i < pieces.length; i++){
pieces[i] = decode(pieces[i]);
}
return pieces;
};

var decode = function(str){
try {
return decodeURIComponent(str);
} catch (ex) {
return querystring.unescape(str);
}
};

var portString = function(o){
if (o.protocol() === 'https'){
Expand Down
Loading

0 comments on commit 1a2b51f

Please sign in to comment.