Skip to content
Myr

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

6 files+22 added−4 removed

modified:deno_dist/router/linear-router/router.ts+1 added-1 removed
modified:deno_dist/router/pattern-router/router.ts+1 added-1 removed
modified:src/router/linear-router/router.test.ts+9 added
88 unmodified lines
89 expect(res[0][0]).toEqual('auth middleware')90 expect(res[1][0]).toEqual('posts')91 expect(res[2][0]).toEqual('fallback')92 })9394 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})103104describe('Multi match', () => {105 const router = new LinearRouter<string>()
256 unmodified lines
modified:src/router/linear-router/router.ts+1 added-1 removed
4 unmodified lines
5type RegExpMatchArrayWithIndices = RegExpMatchArray & { indices: [number, number][] }67const emptyParams = {}89const splitPathRe = /\/(:\w+(?:{[^}]+})?)|\/[^\/\?]+|(\?)/g9const splitPathRe = /\/(:\w+(?:{(?:(?:{[\d,]+})|[^}])+})?)|\/[^\/\?]+|(\?)/g10const splitByStarRe = /\*/11export class LinearRouter<T> implements Router<T> {12 name: string = 'LinearRouter'13 routes: [string, string, T][] = []
119 unmodified lines
modified:src/router/pattern-router/router.test.ts+9 added
76 unmodified lines
77 expect(res.length).toBe(0)78 ;[res] = router.match('GET', '/post/123/123')79 expect(res.length).toBe(0)80 })8182 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})9192describe('Multi match', () => {93 const router = new PatternRouter<string>()
486 unmodified lines
modified:src/router/pattern-router/router.ts+1 added-1 removed
11 unmodified lines
12 if (endsWithWildcard) {13 path = path.slice(0, -2)14 }1516 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