Skip to content

Commit

Permalink
fix: router parameter matching for paths with /.* (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
BelgianNoise authored Mar 3, 2022
1 parent 56fe829 commit be85643
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions packages/dgt-utils/lib/router/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ describe('Router', () => {

});

it('should not match greedy on /.* routes', async () => {

delete window.location;
(window.location as any) = new URL('http://localhost/onePart/12345/filler/testing?search=test#test');

const testRoute: Route = {
path: '/{{partOne}}/{{numbers}}/.*',
targets: [],
};

expect(urlVariables(testRoute).pathParams.get('numbers')).toBe('12345');

});

it('should error when no match was found for every variable', () => {

const invalidRoute: Route = {
Expand Down
2 changes: 1 addition & 1 deletion packages/dgt-utils/lib/router/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export const urlVariables = (route: Route): UrlVariables => {
const searchParams = new URL(window.location.href).searchParams;
const hash = new URL(window.location.href).hash;

const regex = new RegExp(`^${route.path.replace(/{{[^/]+}}/ig, '(.+)')}$`, 'i');
const regex = new RegExp(`^${route.path.replace(/{{[^/]+}}/ig, '(.+?)')}$`, 'i');

const parts = route.path.split('/')
.filter((part) => part.startsWith('{{') && part.endsWith('}}'));
Expand Down

0 comments on commit be85643

Please sign in to comment.