Skip to content

Commit

Permalink
Provide all URL parameters to each route (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzzy authored and langpavel committed Oct 6, 2016
1 parent e3c51de commit f3a7c2f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/matchPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function decodeParam(val) {
}
}

function matchPathBase(end, routePath, urlPath) {
function matchPathBase(end, routePath, urlPath, parentParams) {
const key = `${routePath}|${end}`;
let regexp = cache.get(key);

Expand All @@ -45,6 +45,10 @@ function matchPathBase(end, routePath, urlPath) {
}

const params = Object.create(null);
if (parentParams) {
Object.assign(params, parentParams);
}

const path = m[0];

for (let i = 1; i < m.length; i += 1) {
Expand Down
9 changes: 5 additions & 4 deletions src/matchRoute.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

import { matchPath, matchBasePath } from './matchPath';

function* matchRoute(route, baseUrl, path) {
function* matchRoute(route, baseUrl, path, parentParams) {
let match;

if (!route.children) {
match = matchPath(route.path, path);
match = matchPath(route.path, path, parentParams);

if (match) {
yield {
Expand All @@ -27,7 +27,7 @@ function* matchRoute(route, baseUrl, path) {
}

if (route.children) {
match = matchBasePath(route.path, path);
match = matchBasePath(route.path, path, parentParams);
if (match) {
yield {
route,
Expand All @@ -42,7 +42,8 @@ function* matchRoute(route, baseUrl, path) {
yield* matchRoute(
childRoute,
baseUrl + (match.path === '/' ? '' : match.path),
newPath.startsWith('/') ? newPath : `/${newPath}`
newPath.startsWith('/') ? newPath : `/${newPath}`,
match.params
);
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/resolve.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,27 @@ describe('resolve(routes, { path, ...context })', () => {
expect(action.args[0][0]).to.have.deep.property('params.two', 'b');
});

it('should provide all URL parameters to each route', async () => {
const action = sinon.spy();
const routes = [
{
path: '/:one',
children: [
{
path: '/:two',
action,
},
],
action,
},
];
await resolve(routes, { path: '/a/b' });
expect(action.calledTwice).to.be.true;
expect(action.args[0][0]).to.have.deep.property('params.one', 'a');
expect(action.args[1][0]).to.have.deep.property('params.one', 'a');
expect(action.args[1][0]).to.have.deep.property('params.two', 'b');
});

it('should support next() across multiple routes', async () => {
const log = [];
const routes = [
Expand Down

0 comments on commit f3a7c2f

Please sign in to comment.