Skip to content

Commit 9cf764e

Browse files
committed
feat(UrlMatcher): handle query string arrays
UrlMatcher now correctly handles query string array values coming from $location, and properly encodes them to URLs, following $location's convention. Closes #373
1 parent ebd68d7 commit 9cf764e

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/urlMatcherFactory.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ UrlMatcher.prototype.format = function (values) {
301301
if (!values) return segments.join('').replace('//', '/');
302302

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

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

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

318318
for (/**/; i < nTotal; i++) {
319319
param = params[i];
320-
if (values[param] == null) continue;
321-
result += (search ? '&' : '?') + param + '=' + encodeURIComponent(values[param]);
320+
value = values[param];
321+
if (value == null) continue;
322+
array = isArray(value);
323+
324+
if (array) {
325+
value = value.map(encodeURIComponent).join('&' + param + '=');
326+
}
327+
result += (search ? '&' : '?') + param + '=' + (array ? value : encodeURIComponent(value));
322328
search = true;
323329
}
324330
return result;

test/urlMatcherFactorySpec.js

+7-7
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,13 @@ describe("UrlMatcher", function () {
5555

5656
it("should parse parameter placeholders", function () {
5757
var matcher = new UrlMatcher('/users/:id/details/{type}/{repeat:[0-9]+}?from&to');
58-
var params = matcher.parameters();
59-
expect(params.length).toBe(5);
60-
expect(params).toContain('id');
61-
expect(params).toContain('type');
62-
expect(params).toContain('repeat');
63-
expect(params).toContain('from');
64-
expect(params).toContain('to');
58+
expect(matcher.parameters()).toEqual(['id', 'type', 'repeat', 'from', 'to']);
59+
});
60+
61+
it("should encode and decode duplicate query string values as array", function () {
62+
var matcher = new UrlMatcher('/?foo'), array = { foo: ["bar", "baz"] };
63+
expect(matcher.exec('/', array)).toEqual(array);
64+
expect(matcher.format(array)).toBe('/?foo=bar&foo=baz');
6565
});
6666

6767
describe("snake-case parameters", function() {

0 commit comments

Comments
 (0)