Skip to content

Commit

Permalink
adding callback argument to .query() - closes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
rodneyrehm committed Feb 11, 2013
1 parent 18660e4 commit d7463c7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ URI.js is published under the [MIT license](http://www.opensource.org/licenses/m
## Changelog ##

* adding [`.setQuery()`](http://medialize.github.com/URI.js/docs.html#search-set) - ([Issue #64](https://github.com/medialize/URI.js/issues/64))
* adding callback argument to [`.query()`](http://medialize.github.com/URI.js/docs.html#accessors-search)


### 1.8.3 (January 9th 2013) ###

Expand Down
12 changes: 12 additions & 0 deletions docs.html
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,18 @@ <h3 id="accessors-search">search(), query()</h3>
uri.search({ foo: "bar", hello : ["world", "mars"] });
// uri == "http://example.org/bar/world.html?foo=bar&amp;hello=world&amp;hello=mars"

// overwrite data through callback
uri.search(function(data) {
return { hello : "world" };
});
// uri == "http://example.org/bar/world.html?hello=world"

// augment data through callback
uri.search(function(data) {
data.foo = "bar";
});
// uri == "http://example.org/bar/world.html?hello=world&amp;foo=bar"

// CAUTION: beware of arrays, the following are not quite the same
// If you're dealing with PHP, you probably want the latter…
uri.search("?foo=bar&amp;bar=baz");
Expand Down
6 changes: 6 additions & 0 deletions src/URI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,12 @@ var q = p.query;
p.query = function(v, build) {
if (v === true) {
return URI.parseQuery(this._parts.query);
} else if (typeof v === "function") {
var data = URI.parseQuery(this._parts.query);
var result = v.call(this, data);
this._parts.query = URI.buildQuery(result || data, this._parts.duplicateQueryParameters);
this.build(!build);
return this;
} else if (v !== undefined && typeof v !== "string") {
this._parts.query = URI.buildQuery(v, this._parts.duplicateQueryParameters);
this.build(!build);
Expand Down
16 changes: 15 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,21 @@ test("mutating object", function() {
u.query(q);
equal(u.query(), 'something=new&something=and&something=funky', "removing array");
});
test("addQuery", function() {
test("query callback", function() {
var u = URI('?foo=bar');
u.query(function(data) {
data.foo = "bam";
});
equal(u.query(), 'foo=bam', "augment argument");

u.query(function(data) {
return {
bla: 'blubb'
}
});
equal(u.query(), 'bla=blubb', "overwrite returned value");
});
test("setQuery", function() {
var u = URI('?foo=bar');
u.setQuery('foo', 'bam');
equal(u.query(), 'foo=bam', "set name, value");
Expand Down

0 comments on commit d7463c7

Please sign in to comment.