Skip to content

Commit

Permalink
feat(route-recognizer): Support multiple names per route
Browse files Browse the repository at this point in the history
add support for the route name parameter to be an array of names, each of which can be resolved to the route, whereas previously an array was accepted but converted into a string repr
  • Loading branch information
aivins committed Feb 25, 2016
1 parent 4d18463 commit 6b5637d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/route-recognizer.js
Expand Up @@ -64,10 +64,13 @@ export class RouteRecognizer {
let handlers = [{ handler: route.handler, names: names }];

if (routeName) {
this.names[routeName] = {
segments: segments,
handlers: handlers
};
let routeNames = Array.isArray(routeName) ? routeName : [routeName];
for (let i = 0; i < routeNames.length; i++) {
this.names[routeNames[i]] = {
segments: segments,
handlers: handlers
};
}
}

currentState.handlers = handlers;
Expand Down
9 changes: 9 additions & 0 deletions test/route-recognizer.spec.js
Expand Up @@ -3,6 +3,7 @@ import core from 'core-js';

const staticRoute = {'path': 'static','handler': {'name': 'static'}};
const dynamicRoute = {'path': 'dynamic/:id','handler': {'name': 'dynamic'}};
const multiNameRoute = {'path': 'static','handler': {'name': ['static-multiple', 'static-multiple-alias']}};

const routeTestData = [{
title: 'empty path routes',
Expand Down Expand Up @@ -117,4 +118,12 @@ describe('route recognizer', () => {
expect(recognizer.hasRoute('static')).toBe(true);
expect(recognizer.handlersFor('static')[0].handler).toEqual(staticRoute.handler);
});

it('should find a handler by multiple names', () => {
let recognizer = new RouteRecognizer();
recognizer.add([multiNameRoute]);

expect(recognizer.handlersFor('static-multiple')[0].handler)
.toEqual(recognizer.handlersFor('static-multiple-alias')[0].handler)
});
});

0 comments on commit 6b5637d

Please sign in to comment.