Skip to content

Commit

Permalink
fix bugs with setting query string
Browse files Browse the repository at this point in the history
  • Loading branch information
peterkhayes committed Jun 24, 2016
1 parent 4a58e01 commit fdfff46
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 94 deletions.
56 changes: 24 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ if (!Array.isArray) {
};
}

var isEmpty = function(val) {
return val === null || val === undefined || val === false;
};

var objectEach = function(obj, cb){
for (var k in obj){
if (obj.hasOwnProperty(k)) {
Expand Down Expand Up @@ -100,7 +104,7 @@ UrlGrey.prototype.parsed = function(){
path: this._parsed.path,
href: this._parsed.href,
username: this._parsed.username,
password: this._parsed.password
password: this._parsed.password
};


Expand Down Expand Up @@ -154,22 +158,10 @@ UrlGrey.prototype.port = function(num){
};

UrlGrey.prototype.query = function(mergeObject){
var path;
if (mergeObject === false){
// clear the query entirely if the input === false
if (arguments.length === 0) {
return querystring.parse(this.queryString());
} else if (isEmpty(mergeObject)){
return this.queryString('');
}

var url = this.url;
if (!mergeObject){
var parsed = urlParse(url);
if (!!parsed.search){
var qstr = parsed.search.substring(1);
var output = querystring.parse(qstr);

return output;
}
return {};
} else {
// read the object out
var oldQuery = querystring.parse(this.queryString());
Expand All @@ -181,26 +173,25 @@ UrlGrey.prototype.query = function(mergeObject){
}
});
var newString = querystring.stringify(oldQuery);
return this.queryString(newString);
var ret = this.queryString(newString);
return ret;
}
};


UrlGrey.prototype.rawQuery = function(mergeObject){
var path;
if (mergeObject === false){
// clear the query entirely if the input === false
if (arguments.length === 0) {
if (this.queryString().length === 0) { return {}; }

return this.queryString().split("&").reduce(function(obj, pair) {
pair = pair.split("=");
var key = pair[0];
var val = pair[1];
obj[key] = val;
return obj;
}, {});
} else if (isEmpty(mergeObject)){
return this.queryString('');
}

var url = this.url;
if (!mergeObject){
var parsed = urlParse(url);
if (!!parsed.search){
var qstr = parsed.search.substring(1);
return querystring.parse(qstr);
}
return {};
} else {
// read the object out
var oldQuery = querystring.parse(this.queryString());
Expand Down Expand Up @@ -441,12 +432,13 @@ function addPropertyGetterSetter(propertyName, methodName){
methodName = propertyName;
}
UrlGrey.prototype[methodName] = function(str){
if (!!str || str === ''){
if (isEmpty(str)){
return this.parsed()[propertyName];
} else {
var obj = new UrlGrey(this.toString());
obj.parsed()[propertyName] = str;
return obj;
}
return this.parsed()[propertyName];
};
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"url",
"uri"
],
"version": "0.4.2",
"version": "0.4.3",
"bugs": {
"url": "https://github.com/cainus/urlgrey/issues"
},
Expand Down
82 changes: 54 additions & 28 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,50 +314,76 @@ describe("urlgrey", function(){
});
describe("#rawQuery", function(){
it("adds a querystring", function(){
urlgrey("http://asdf.com").rawQuery({asdf:'12 34'})
.toString().should.equal("http://asdf.com?asdf=12 34");
var updated = urlgrey("http://asdf.com").rawQuery({foo:'12 34'});

updated.toString().should.equal("http://asdf.com?foo=12 34");
updated.rawQuery().should.deep.equal({foo: "12 34"});
});
it("appends a querystring", function(){
var updated = urlgrey("http://asdf.com?foo=1234").rawQuery({bar:'56 78'});

updated.toString().should.equal("http://asdf.com?foo=1234&bar=56 78");
updated.rawQuery().should.deep.equal({foo: "1234", bar: "56 78"});
});
it("modifies a querystring", function(){
urlgrey("http://asdf.com?asdf=5678&b=2").rawQuery({asdf:'12 34'})
.toString().should.equal("http://asdf.com?asdf=12 34&b=2");
var updated = urlgrey("http://asdf.com?foo=1234&bar=abcd").rawQuery({foo: "56 78"});

updated.toString().should.equal("http://asdf.com?foo=56 78&bar=abcd");
updated.rawQuery().should.deep.equal({foo: "56 78", bar: "abcd"});
});
it("clears a querystring", function(){
urlgrey("http://asdf.com?asdf=5678").rawQuery(false)
.toString().should.equal("http://asdf.com");
});
it("clears an element of a querystring", function(){
urlgrey("http://asdf.com?a=1&b=2&c=3&d=4")
.rawQuery({a: 0, b: null, c: false, d: "false", e: "12 34"})
.toString().should.equal("http://asdf.com?a=0&d=false&e=12 34");
var updated = urlgrey("http://asdf.com?foo=1234").rawQuery(false);

updated.toString().should.equal("http://asdf.com");
updated.rawQuery().should.deep.equal({});
});
it("clears an element of a querystring with null or false", function(){
var updated = urlgrey("http://asdf.com")
.rawQuery({foo: 1, bar: 2, baz: 3})
.rawQuery({foo: 0, bar: null, baz: false});

updated.toString().should.equal("http://asdf.com?foo=0");
updated.rawQuery().should.deep.equal({foo: "0"});
});
it("extracts a querystring as an object", function(){
chai.expect(
urlgrey("http://asdf.com?asdf=56%2078").rawQuery()
).to.eql({asdf:'56 78'});
urlgrey("http://asdf.com?asdf=56%2078").rawQuery().should.deep.equal({asdf:'56%2078'});
});
});
describe("#query", function(){
it("adds a querystring", function(){
urlgrey("http://asdf.com").query({asdf:'12 34'})
.toString().should.equal("http://asdf.com?asdf=12%2034");
var updated = urlgrey("http://asdf.com").query({foo:'12 34'});

updated.toString().should.equal("http://asdf.com?foo=12%2034");
updated.query().should.deep.equal({foo: "12 34"});
});
it("appends a querystring", function(){
var updated = urlgrey("http://asdf.com?foo=1234").query({bar:'56 78'});

updated.toString().should.equal("http://asdf.com?foo=1234&bar=56%2078");
updated.query().should.deep.equal({foo: "1234", bar: "56 78"});
});
it("modifies a querystring", function(){
urlgrey("http://asdf.com?asdf=5678&b=2").query({asdf:1234})
.toString().should.equal("http://asdf.com?asdf=1234&b=2");
var updated = urlgrey("http://asdf.com?foo=1234&bar=abcd").query({foo: "56 78"});

updated.toString().should.equal("http://asdf.com?foo=56%2078&bar=abcd");
updated.query().should.deep.equal({foo: "56 78", bar: "abcd"});
});
it("clears a querystring", function(){
urlgrey("http://asdf.com?asdf=5678").query(false)
.toString().should.equal("http://asdf.com");
});
it("clears an element of a querystring", function(){
urlgrey("http://asdf.com?a=1&b=2&c=3&d=4")
.query({a: 0, b: null, c: false, d: "false", e: "12 34"})
.toString().should.equal("http://asdf.com?a=0&d=false&e=12%2034");
var updated = urlgrey("http://asdf.com?foo=1234").query(false);

updated.toString().should.equal("http://asdf.com");
updated.query().should.deep.equal({});
});
it("clears an element of a querystring with null or false", function(){
var updated = urlgrey("http://asdf.com")
.query({foo: 1, bar: 2, baz: 3})
.query({foo: 0, bar: null, baz: false});

updated.toString().should.equal("http://asdf.com?foo=0");
updated.query().should.deep.equal({foo: "0"});
});
it("extracts a querystring as an object", function(){
chai.expect(
urlgrey("http://asdf.com?asdf=56%2078").query()
).to.eql({asdf:'56 78'});
urlgrey("http://asdf.com?asdf=56%2078").query().should.deep.equal({asdf:'56 78'});
});
});
describe('#encode', function(){
Expand Down
56 changes: 24 additions & 32 deletions urlgrey.js
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,10 @@ if (!Array.isArray) {
};
}

var isEmpty = function(val) {
return val === null || val === undefined || val === false;
};

var objectEach = function(obj, cb){
for (var k in obj){
if (obj.hasOwnProperty(k)) {
Expand Down Expand Up @@ -676,7 +680,7 @@ UrlGrey.prototype.parsed = function(){
path: this._parsed.path,
href: this._parsed.href,
username: this._parsed.username,
password: this._parsed.password
password: this._parsed.password
};


Expand Down Expand Up @@ -730,22 +734,10 @@ UrlGrey.prototype.port = function(num){
};

UrlGrey.prototype.query = function(mergeObject){
var path;
if (mergeObject === false){
// clear the query entirely if the input === false
if (arguments.length === 0) {
return querystring.parse(this.queryString());
} else if (isEmpty(mergeObject)){
return this.queryString('');
}

var url = this.url;
if (!mergeObject){
var parsed = urlParse(url);
if (!!parsed.search){
var qstr = parsed.search.substring(1);
var output = querystring.parse(qstr);

return output;
}
return {};
} else {
// read the object out
var oldQuery = querystring.parse(this.queryString());
Expand All @@ -757,26 +749,25 @@ UrlGrey.prototype.query = function(mergeObject){
}
});
var newString = querystring.stringify(oldQuery);
return this.queryString(newString);
var ret = this.queryString(newString);
return ret;
}
};


UrlGrey.prototype.rawQuery = function(mergeObject){
var path;
if (mergeObject === false){
// clear the query entirely if the input === false
if (arguments.length === 0) {
if (this.queryString().length === 0) { return {}; }

return this.queryString().split("&").reduce(function(obj, pair) {
pair = pair.split("=");
var key = pair[0];
var val = pair[1];
obj[key] = val;
return obj;
}, {});
} else if (isEmpty(mergeObject)){
return this.queryString('');
}

var url = this.url;
if (!mergeObject){
var parsed = urlParse(url);
if (!!parsed.search){
var qstr = parsed.search.substring(1);
return querystring.parse(qstr);
}
return {};
} else {
// read the object out
var oldQuery = querystring.parse(this.queryString());
Expand Down Expand Up @@ -1017,12 +1008,13 @@ function addPropertyGetterSetter(propertyName, methodName){
methodName = propertyName;
}
UrlGrey.prototype[methodName] = function(str){
if (!!str || str === ''){
if (isEmpty(str)){
return this.parsed()[propertyName];
} else {
var obj = new UrlGrey(this.toString());
obj.parsed()[propertyName] = str;
return obj;
}
return this.parsed()[propertyName];
};
}

Expand Down
Loading

0 comments on commit fdfff46

Please sign in to comment.