Skip to content

Commit 1d2a89b

Browse files
committedJun 24, 2015
Merge pull request #27 from gigabo/array-path-with-name-collision
Support array paths with parameter name collisions
2 parents 47b0c3d + f8ce044 commit 1d2a89b

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed
 

‎lib/router.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,12 @@ Route.prototype.match = function (url, options) {
120120
// 4. method/path/navParams all matched, extract the matched path params
121121
var routeParams = {};
122122
for (i = 0, len = self.keys.length; i < len; i++) {
123-
routeParams[self.keys[i].name] = pathMatches[i+1];
123+
// Don't overwrite a previously populated parameter with `undefined`.
124+
// A route may legitimately have multiple instances of a parameter
125+
// name if the path was an array.
126+
if (pathMatches[i+1] !== undefined || routeParams[self.keys[i].name] === undefined){
127+
routeParams[self.keys[i].name] = pathMatches[i+1];
128+
}
124129
}
125130

126131
return routeParams;

‎tests/unit/lib/router.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ var routes = {
4848
array_path: {
4949
path: ['/array_path']
5050
},
51+
array_path_name_collision: {
52+
path: [
53+
'/array/path/with/collision/foo/:key',
54+
'/array/path/with/collision/bar/:key'
55+
],
56+
method: 'GET',
57+
page: 'arrayPathNameCollision'
58+
},
5159
invalid_path: {
5260
path: 123
5361
}
@@ -61,7 +69,7 @@ describe('Router', function () {
6169

6270
describe('#constructor', function () {
6371
it('should init correctly', function () {
64-
expect(Object.keys(router._routes).length).to.equal(9);
72+
expect(Object.keys(router._routes).length).to.equal(10);
6573

6674
expect(router._routes.article.name).to.equal('article');
6775
expect(router._routes.article.config.path).to.equal('/:site/:category?/:subcategory?/:alias');
@@ -120,10 +128,10 @@ describe('Router', function () {
120128
process.env.NODE_ENV = 'production';
121129
var notFrozen = new Router(routes);
122130

123-
expect(Object.keys(notFrozen._routes).length).to.equal(9);
131+
expect(Object.keys(notFrozen._routes).length).to.equal(10);
124132
notFrozen._routes.foo = null;
125133
expect(notFrozen._routes.foo).to.equal(null);
126-
expect(Object.keys(notFrozen._routes).length).to.equal(10);
134+
expect(Object.keys(notFrozen._routes).length).to.equal(11);
127135

128136
var homeRoute = notFrozen._routes.home;
129137
expect(homeRoute.name).to.equal('home');
@@ -136,7 +144,7 @@ describe('Router', function () {
136144
process.env.NODE_ENV = 'development';
137145
var frozen = new Router(routes);
138146
var homeRoute = frozen._routes.home;
139-
expect(Object.keys(frozen._routes).length).to.equal(9);
147+
expect(Object.keys(frozen._routes).length).to.equal(10);
140148
expect(homeRoute.name).to.equal('home');
141149
expect(homeRoute.config.path).to.equal('/');
142150
expect(homeRoute.config.method).to.equal('get');
@@ -161,7 +169,7 @@ describe('Router', function () {
161169
expect(function () {
162170
homeRoute.config.regexp = null;
163171
}).to.throw(TypeError);
164-
expect(Object.keys(frozen._routes).length).to.equal(9);
172+
expect(Object.keys(frozen._routes).length).to.equal(10);
165173
expect(frozen._routes.foo).to.equal(undefined);
166174
expect(homeRoute.keys.length).to.equal(0);
167175
expect(homeRoute.name).to.equal('home');
@@ -254,6 +262,15 @@ describe('Router', function () {
254262
route = router.getRoute('/new_article', 'delete');
255263
expect(route).to.equal(null);
256264
});
265+
it('array route with param name collision first', function () {
266+
var route = router.getRoute('/array/path/with/collision/foo/abc');
267+
expect(route.params.key).to.equal('abc');
268+
});
269+
it('array route with param name collision second', function () {
270+
var route = router.getRoute('/array/path/with/collision/bar/abc');
271+
expect(route.params.key).to.equal('abc');
272+
});
273+
257274
});
258275

259276
describe('#makePath', function () {

0 commit comments

Comments
 (0)