Skip to content

Commit

Permalink
feat(UrlMatcher): handle query string arrays
Browse files Browse the repository at this point in the history
UrlMatcher now correctly handles query string array values coming from $location, and properly encodes them to URLs, following $location's convention.

Closes angular-ui#373
  • Loading branch information
nateabele committed Apr 22, 2014
1 parent ebd68d7 commit 9cf764e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
12 changes: 9 additions & 3 deletions src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ UrlMatcher.prototype.format = function (values) {
if (!values) return segments.join('').replace('//', '/');

var nPath = segments.length - 1, nTotal = params.length,
result = segments[0], i, search, value, param, cfg;
result = segments[0], i, search, value, param, cfg, array;

if (!this.validates(values)) return null;

Expand All @@ -317,8 +317,14 @@ UrlMatcher.prototype.format = function (values) {

for (/**/; i < nTotal; i++) {
param = params[i];
if (values[param] == null) continue;
result += (search ? '&' : '?') + param + '=' + encodeURIComponent(values[param]);
value = values[param];
if (value == null) continue;
array = isArray(value);

if (array) {
value = value.map(encodeURIComponent).join('&' + param + '=');
}
result += (search ? '&' : '?') + param + '=' + (array ? value : encodeURIComponent(value));
search = true;
}
return result;
Expand Down
14 changes: 7 additions & 7 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ describe("UrlMatcher", function () {

it("should parse parameter placeholders", function () {
var matcher = new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to');
var params = matcher.parameters();
expect(params.length).toBe(5);
expect(params).toContain('id');
expect(params).toContain('type');
expect(params).toContain('repeat');
expect(params).toContain('from');
expect(params).toContain('to');
expect(matcher.parameters()).toEqual(['id', 'type', 'repeat', 'from', 'to']);
});

it("should encode and decode duplicate query string values as array", function () {
var matcher = new UrlMatcher('/?foo'), array = { foo: ["bar", "baz"] };
expect(matcher.exec('/', array)).toEqual(array);
expect(matcher.format(array)).toBe('/?foo=bar&foo=baz');
});

describe("snake-case parameters", function() {
Expand Down

0 comments on commit 9cf764e

Please sign in to comment.