Skip to content
This repository was archived by the owner on Jun 27, 2022. It is now read-only.

Commit 80d8c2e

Browse files
committed
Allows zero characters match for a wildcard
1 parent c94d283 commit 80d8c2e

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/pathToRegExp.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ describe('pathToRegExp', () => {
3333
describe('given a path with a wildcard', () => {
3434
it('should handle wildcard', () => {
3535
const exp = pathToRegExp('/user/*/shipping')
36-
expect(exp).toEqual(/\/user\/.+\/shipping(\/|$)/gi)
36+
expect(exp).toEqual(/\/user\/.*\/shipping(\/|$)/gi)
3737
})
3838
})
3939

4040
describe('given a path with parameter and wildcard', () => {
4141
it('should handle both', () => {
4242
const exp = pathToRegExp('/user/:userId/*')
43-
expect(exp).toEqual(/\/user\/(?<userId>[^/]+?)\/.+(\/|$)/gi)
43+
expect(exp).toEqual(/\/user\/(?<userId>[^/]+?)\/.*(\/|$)/gi)
4444
})
4545
})
4646

src/pathToRegExp.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ export const pathToRegExp = (path: string): RegExp => {
1212
.replace(/\?/g, '\\?')
1313
// Ignore trailing slashes
1414
.replace(/\/+$/, '')
15-
// Replace wildcard with any single character sequence
16-
.replace(/\*+/g, '.+')
15+
// Replace wildcard with any zero-to-any character sequence
16+
.replace(/\*+/g, '.*')
1717
// Replace parameters with named capturing groups
1818
.replace(
1919
/:([^\d|^\/][a-zA-Z0-9_]*(?=(?:\/|\\.)|$))/g,
20-
(_, match) => `(?<${match}>[^\/]+?)`,
20+
(_, paramName) => `(?<${paramName}>[^\/]+?)`,
2121
)
2222
// Allow optional trailing slash
2323
.concat('(\\/|$)')

test/path.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,25 @@ runner('Path', [
288288
},
289289
],
290290
},
291+
{
292+
given: '/user/details*',
293+
when: [
294+
{
295+
actual: '/user/details',
296+
it: {
297+
matches: true,
298+
params: null,
299+
},
300+
},
301+
{
302+
actual: '/user/details/arbitrary',
303+
it: {
304+
matches: true,
305+
params: null,
306+
},
307+
},
308+
],
309+
},
291310

292311
/**
293312
* Parameter and wildcard

0 commit comments

Comments
 (0)