Skip to content

Commit

Permalink
fixing issue #46 - .addQuery("empty") wonldn't add "?empty"
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Oct 30, 2012
1 parent 01cfbc6 commit 6e942cb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,9 @@ <h3 id="search-add">addSearch(), addQuery()</h3>
uri.addSearch({ foo: "bar", goodbye : ["world", "mars"] });
// uri == "?hello=world&amp;hello=mars&amp;foo=bar&amp;goodbye=world&amp;goodbye=mars"

uri.addSearch("no-value");
// uri == "?hello=world&amp;hello=mars&amp;foo=bar&amp;goodbye=world&amp;goodbye=mars&amp;no-value"

// CAUTION: beware of arrays, the following are not quite the same
// If you're dealing with PHP, you probably want the latter…
uri.addSearch("foo", ["bar", "baz"]);
Expand Down
4 changes: 2 additions & 2 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -815,7 +815,7 @@ p.hostname = function(v, build) {
return _hostname.call(this, v, build);
};

// combination accessors
// compound accessors
p.host = function(v, build) {
if (this._parts.urn) {
return v === undefined ? '' : this;
Expand Down Expand Up @@ -1207,7 +1207,7 @@ p.query = function(v, build) {
};
p.addQuery = function(name, value, build) {
var data = URI.parseQuery(this._parts.query);
URI.addQuery(data, name, value);
URI.addQuery(data, name, value === undefined ? null : value);
this._parts.query = URI.buildQuery(data);
if (typeof name !== "string") {
build = value;
Expand Down
8 changes: 8 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,14 @@ test("addQuery", function() {
u.addQuery({'bam': null, 'baz': ''});
equal(u.query(), 'foo=bar&bam&baz=', "add {name: null}");

u.query('?foo=bar');
u.addQuery('empty');
equal(u.query(), 'foo=bar&empty', "add undefined");

u.query('?foo=bar');
u.addQuery('empty', "");
equal(u.query(), 'foo=bar&empty=', "add empty string");

u.query('');
u.addQuery('some value', "must be encoded because of = and ? and #");
equal(u.query(), 'some+value=must+be+encoded+because+of+%3D+and+%3F+and+%23', "encoding");
Expand Down

0 comments on commit 6e942cb

Please sign in to comment.