fix(router): LinearRouter and PatternRouter support regexp quantifiers (#2209)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* fix(router): LinearRouter and PatternRouter support regexp quantifiers * denoify
authored by Yusuke Wada · committed by GitHub · parent 048f33a · Browse files · 6 files·+22 added−4 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+22 added−4 removed
- tests:+18 added−0 removed
- other:+4 added−4 removed
Provenance
- Commit
fd32be1- Tree
b1f95d8- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
048f33a
6 files changed+22 added−4 removed
modified:deno_dist/router/linear-router/router.ts+1 added-1 removed
4 unmodified lines56789910111213119 unmodified linesExpand all4 unmodified linestype RegExpMatchArrayWithIndices = RegExpMatchArray & { indices: [number, number][] }
const emptyParams = {}
const splitPathRe = /\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/gconst splitPathRe = /\/(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/[^\/\?]+|(\?)/gconst splitByStarRe = /\*/export class LinearRouter<T> implements Router<T> { name: string = 'LinearRouter' routes: [string, string, T][] = []119 unmodified linesExpand allmodified:deno_dist/router/pattern-router/router.ts+1 added-1 removed
11 unmodified lines1213141516161718192034 unmodified lines11 unmodified lines if (endsWithWildcard) { path = path.slice(0, -2) }
const parts = path.match(/\/?(:\w+(?:{[^}]+})?)|\/?[^\/\?]+|(\?)/g) || [] const parts = path.match(/\/?(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/?[^\/\?]+|(\?)/g) || [] if (parts[parts.length - 1] === '?') { this.add(method, parts.slice(0, parts.length - 2).join(''), handler) parts.pop() }34 unmodified linesmodified:src/router/linear-router/router.test.ts+9 added
88 unmodified lines89 expect(res[0][0]).toEqual('auth middleware')90 expect(res[1][0]).toEqual('posts')91 expect(res[2][0]).toEqual('fallback')92 })93+94+ it('Should support regexp quantifiers', async () => {95+ router.add('GET', '/year/:year{[0-9]{4}}/month/:month{[0-9]{1,2}}', 'handler')96+ const [res] = router.match('GET', '/year/2024/month/2')97+ expect(res.length).toBe(1)98+ expect(res[0][0]).toBe('handler')99+ expect(res[0][1]['year']).toBe('2024')100+ expect(res[0][1]['month']).toBe('2')101+ })102 })103 104 describe('Multi match', () => {105 const router = new LinearRouter<string>()256 unmodified linesmodified:src/router/linear-router/router.ts+1 added-1 removed
4 unmodified lines5 type RegExpMatchArrayWithIndices = RegExpMatchArray & { indices: [number, number][] }6 7 const emptyParams = {}8 9-const splitPathRe = /\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g9+const splitPathRe = /\/(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/[^\/\?]+|(\?)/g10 const splitByStarRe = /\*/11 export class LinearRouter<T> implements Router<T> {12 name: string = 'LinearRouter'13 routes: [string, string, T][] = []119 unmodified linesmodified:src/router/pattern-router/router.test.ts+9 added
76 unmodified lines77 expect(res.length).toBe(0)78 ;[res] = router.match('GET', '/post/123/123')79 expect(res.length).toBe(0)80 })81+82+ it('Should support regexp quantifiers', async () => {83+ router.add('GET', '/year/:year{[0-9]{4}}/month/:month{[0-9]{1,2}}', 'handler')84+ const [res] = router.match('GET', '/year/2024/month/2')85+ expect(res.length).toBe(1)86+ expect(res[0][0]).toBe('handler')87+ expect(res[0][1]['year']).toBe('2024')88+ expect(res[0][1]['month']).toBe('2')89+ })90 })91 92 describe('Multi match', () => {93 const router = new PatternRouter<string>()486 unmodified linesmodified:src/router/pattern-router/router.ts+1 added-1 removed
11 unmodified lines12 if (endsWithWildcard) {13 path = path.slice(0, -2)14 }15 16- const parts = path.match(/\/?(:\w+(?:{[^}]+})?)|\/?[^\/\?]+|(\?)/g) || []16+ const parts = path.match(/\/?(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/?[^\/\?]+|(\?)/g) || []17 if (parts[parts.length - 1] === '?') {18 this.add(method, parts.slice(0, parts.length - 2).join(''), handler)19 parts.pop()20 }34 unmodified lines