Skip to content
This repository has been archived by the owner on Mar 21, 2019. It is now read-only.

Commit

Permalink
group in route pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
michalkvasnicak committed Dec 6, 2015
1 parent 6aba7d6 commit 070830b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/utils/routeUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 54,19 @@ export function buildMatcher(pathPattern, basePath = '/') {
}

const vars = {};
let indexInMatch = 1;

variableNames.forEach((name, index) => {
vars[name] = matched[index 1];
const start = variablePatterns[index][0];
const end = variablePatterns[index].slice(-1);

if (start === '(' && end === ')') {
vars[name] = matched[indexInMatch];
indexInMatch = 2; // skip nested group
return;
}

vars[name] = matched[indexInMatch ];
});

return {
Expand Down
39 changes: 39 additions & 0 deletions test/Route.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 180,45 @@ describe('Route', () => {
);
});

it('resolves complex route with complex children', () => {
const onEnter = () => {};
const onLeave = () => {};
const children = [
{
path: '/detail/:id{[a-zA-Z0-9] }-:slug',
component: 'b',
onEnter,
onLeave
}
];
const eagerlyMatchedRoute = new Route('/:lang{(en|de)}', '', children, onEnter, onLeave, 'a');

return eagerlyMatchedRoute.match('/en/detail/565ee0d31709ae7b174eb8a1-test').then(
(match) => {
expect(match).be.an('object');
expect(match)
.to.have.property('vars')
.and.to.be.deep.equal({
lang: 'en',
id: '565ee0d31709ae7b174eb8a1',
slug: 'test'
});
expect(match)
.to.have.property('onEnter')
.and.to.be.an('array')
.and.to.be.deep.equal([onEnter, onEnter]);
expect(match)
.to.have.property('onLeave')
.and.to.be.an('array')
.and.to.be.deep.equal([onLeave, onLeave]);
expect(match)
.to.have.property('components')
.and.to.be.an('array')
.and.to.be.deep.equal(['a', 'b']);
}
);
});

it('resolves route with async children', () => {
const asyncRoutes = () => {
return Promise.resolve([
Expand Down

0 comments on commit 070830b

Please sign in to comment.