Skip to content

Commit

Permalink
Allow array parameters in query strings. For #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tmeasday committed Jul 18, 2014
1 parent 262e5b1 commit 0e8f324
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
19 changes: 17 additions & 2 deletions lib/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,13 @@ Url.toQueryString = function (queryObject) {
}

_.each(queryObject, function (value, key) {
result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
if (_.isArray(value)) {
_.each(value, function(valuePart) {
result.push(encodeURIComponent(key + '[]') + '=' + encodeURIComponent(valuePart));
});
} else {
result.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
}
});

// no sense in adding a pointless question mark
Expand Down Expand Up @@ -247,7 +253,16 @@ Url.prototype.params = function (path) {
if (queryString) {
_.each(queryString.split('&'), function (paramString) {
paramParts = paramString.split('=');
params[paramParts[0]] = decodeURIComponent(paramParts[1]);
var key = paramParts[0];
var value = decodeURIComponent(paramParts[1]);

if (key.slice(-2) === '[]') {
var key = key.slice(0, -2)
params[key] = params[key] || [];
params[key].push(value)
} else {
params[key] = value;
}
});
}

Expand Down
20 changes: 20 additions & 0 deletions test/url_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,23 @@ Tinytest.add('Url - matching', function (test) {
test.isTrue(path.exec('/commits/123..456'));
*/
});

Tinytest.add('Url - params', function (test) {
var path = new Url(paths.explicit);
test.isUndefined(path.params('/posts').foo);
test.equal(path.params('/posts?foo=bar').foo, 'bar');
test.equal(path.params('/posts?foo[]=bar').foo, ['bar']);
test.equal(path.params('/posts?foo%5B%5D=bar').foo, ['bar']);
test.equal(path.params('/posts?foo[]=bar&foo[]=baz').foo, ['bar', 'baz']);
test.equal(path.params('/posts?foo%5B%5D=bar&foo%5B%5D=baz').foo, ['bar', 'baz']);
});

Tinytest.add('Url - resolve', function (test) {
var path = new Url(paths.explicit);
test.equal(path.resolve({}), '/posts');
test.equal(path.resolve({}, {query: {foo: 'bar'}}), '/posts?foo=bar');
test.equal(path.resolve({}, {query: {foo: ['bar']}}), '/posts?foo%5B%5D=bar');
test.equal(path.resolve({}, {query: {foo: ['bar', 'baz']}}), '/posts?foo%5B%5D=bar&foo%5B%5D=baz');

This comment has been minimized.

Copy link
@tmeasday

tmeasday Jul 18, 2014

Author Contributor

I'm not sure if the [] should be URL encoded or not. Do you have a read on this @cmather?

This comment has been minimized.

Copy link
@dburles

dburles Jan 1, 2015

Contributor

Just saw this note @tmeasday I don't believe it should be (just created a PR). Did you have a reason for encoding it?

This comment has been minimized.

Copy link
@tmeasday

tmeasday Jan 18, 2015

Author Contributor

Not sure. I don't know the exact rules on URL encoding..

This comment has been minimized.

Copy link
@dburles

dburles Jan 18, 2015

Contributor

Seems as though I was wrong about forms, they are escaped.

<form action="http:///example.com" method="get">
  <input type="checkbox" name="foo[]">
  <input type="checkbox" name="foo[]">
  <input type="submit" value="submit">
</form>
// no good resolution of this one
test.equal(path.resolve({}, {query: {foo: []}}), '/posts');
});

0 comments on commit 0e8f324

Please sign in to comment.