Skip to content

Commit

Permalink
fix(router): make addRoute split list of routes into multiple routeco…
Browse files Browse the repository at this point in the history
…nfigs with single route each
  • Loading branch information
fkleuver authored and davismj committed Mar 15, 2018
1 parent fc76fdd commit 0cd5423
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
13 changes: 2 additions & 11 deletions src/router-configuration.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {RouteConfig} from './interfaces';
import {_ensureArrayWithSingleRoutePerConfig} from './util'

/**
* Class used to configure a [[Router]] instance.
Expand Down Expand Up @@ -111,17 +112,7 @@ export class RouterConfiguration {
*/
mapRoute(config: RouteConfig): RouterConfiguration {
this.instructions.push(router => {
let routeConfigs = [];

if (Array.isArray(config.route)) {
for (let i = 0, ii = config.route.length; i < ii; ++i) {
let current = Object.assign({}, config);
current.route = config.route[i];
routeConfigs.push(current);
}
} else {
routeConfigs.push(Object.assign({}, config));
}
let routeConfigs = _ensureArrayWithSingleRoutePerConfig(config);

let navModel;
for (let i = 0, ii = routeConfigs.length; i < ii; ++i) {
Expand Down
7 changes: 7 additions & 0 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {NavigationInstruction} from './navigation-instruction';
import {NavModel} from './nav-model';
import {RouterConfiguration} from './router-configuration';
import {
_ensureArrayWithSingleRoutePerConfig,
_normalizeAbsolutePath,
_createRootedPath,
_resolveUrl} from './util';
Expand Down Expand Up @@ -299,6 +300,12 @@ export class Router {
* @param navModel The [[NavModel]] to use for the route. May be omitted for single-pattern routes.
*/
addRoute(config: RouteConfig, navModel?: NavModel): void {
if (Array.isArray(config.route)) {
let routeConfigs = _ensureArrayWithSingleRoutePerConfig(config);
routeConfigs.forEach(this.addRoute.bind(this));
return;
}

validateRouteConfig(config, this.routes);

if (!('viewPorts' in config) && !config.navigationStrategy) {
Expand Down
16 changes: 16 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,21 @@ export function _resolveUrl(fragment, baseUrl, hasPushState) {
return _createRootedPath(fragment, baseUrl, hasPushState);
}

export function _ensureArrayWithSingleRoutePerConfig(config) {
let routeConfigs = [];

if (Array.isArray(config.route)) {
for (let i = 0, ii = config.route.length; i < ii; ++i) {
let current = Object.assign({}, config);
current.route = config.route[i];
routeConfigs.push(current);
}
} else {
routeConfigs.push(Object.assign({}, config));
}

return routeConfigs;
}

const isRootedPath = /^#?\//;
const isAbsoluteUrl = /^([a-z][a-z0-9+\-.]*:)?\/\//i;
6 changes: 6 additions & 0 deletions test/router.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ describe('the router', () => {
'number2': { moduleId: 'test2' }
}})).not.toThrow();
});

it('should map a routeconfig with an array of routes to multiple routeconfigs with one route each', () => {
router.addRoute({ route: ['test1', 'test2'], moduleId: 'test', nav: true });
expect(router.routes[0].route).toEqual('test1');
expect(router.routes[1].route).toEqual('test2');
});
});

describe('generate', () => {
Expand Down

0 comments on commit 0cd5423

Please sign in to comment.