Skip to content

Commit

Permalink
Provide all URL parameters to each route (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
langpavel authored and frenzzy committed Oct 20, 2016
1 parent 4f69c62 commit c08ae00
Show file tree
Hide file tree
Showing 3 changed files with 57 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 @@ -23,7 +23,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 @@ -40,6 +40,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
47 changes: 47 additions & 0 deletions test/resolve.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,53 @@ 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 override URL parameters with same name in child route', async () => {
const action = sinon.spy();
const routes = [
{
path: '/:one',
children: [
{
path: '/:one',
action,
},
{
path: '/:two',
action,
},
],
action,
},
];
await resolve(routes, { path: '/a/b' });
expect(action.calledThrice).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', 'b');
expect(action.args[2][0]).to.have.deep.property('params.one', 'a');
expect(action.args[2][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 c08ae00

Please sign in to comment.