Skip to content

Commit

Permalink
fix: Wildcard splat matches
Browse files Browse the repository at this point in the history
  • Loading branch information
rschristian committed Oct 19, 2024
1 parent ed00281 commit ec22782
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export const exec = (url, route, matches) => {
if (!m || (!val && flag != '?' && flag != '*')) return;
rest = flag == '+' || flag == '*';
// rest (+/*) match:
if (rest) val = url.slice(i).map(decodeURIComponent).join('/');
if (rest) val = url.slice(i).map(decodeURIComponent).join('/') || undefined;
// normal/optional field:
else if (val) val = decodeURIComponent(val);
matches.params[param] = val;
Expand Down
22 changes: 19 additions & 3 deletions test/node/router-match.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,26 @@ test('Optional param route', () => {
});

test('Optional rest param route "/:x*"', () => {
const accurateResult = execPath('/user', '/user/:id?');
assert.equal(accurateResult, { path: '/user', params: { id: undefined }, id: undefined, query: {} });
const matchedResult = execPath('/user', '/user/:id*');
assert.equal(matchedResult, { path: '/user', params: { id: undefined }, id: undefined, query: {} });

const inaccurateResult = execPath('/', '/user/:id?');
const matchedResultWithSlash = execPath('/user/foo/bar', '/user/:id*');
assert.equal(matchedResultWithSlash, {
path: '/user/foo/bar',
params: { id: 'foo/bar' },
id: 'foo/bar',
query: {}
});

const emptyResult = execPath('/user', '/user/:id*');
assert.equal(emptyResult, {
path: '/user',
params: { id: undefined },
id: undefined,
query: {}
});

const inaccurateResult = execPath('/', '/user/:id*');
assert.equal(inaccurateResult, undefined);
});

Expand Down

0 comments on commit ec22782

Please sign in to comment.