Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Wildcard splat/rest param matches should return undefined instead of empty strings #45

Merged
merged 2 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,22 @@ Paths are matched using a simple string matching algorithm. The following featur
- `:param` - Matches any URL segment, binding the value to the label (can later extract this value from `useRoute()`)
- `/profile/:id` will match `/profile/123` and `/profile/abc`
- `/profile/:id?` will match `/profile` and `/profile/123`
- `/profile/:id*` will match `/profile`, `/profile/123`, and `/profile/123/abc`
- `/profile/:id+` will match `/profile/123`, `/profile/123/abc`
- `*` - Matches one or more URL segments
- `/profile/*` will match `/profile/123`, `/profile/123/abc`, etc.

These can then be composed to create more complex routes:

- `/profile/:id/*` will match `/profile/123/abc`, `/profile/123/abc/def`, etc.

The difference between `/:id*` and `/:id/*` is that in the former, the `id` param will include the entire path after it, while in the latter, the `id` is just the single path segment.

- `/profile/:id*`, with `/profile/123/abc`
- `id` is `123/abc`
- `/profile/:id/*`, with `/profile/123/abc`
- `id` is `123`

### `useLocation`

A hook to work with the `LocationProvider` to access location context.
Expand Down
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