Skip to content
Myr

fix(tire-router/pattern-router): fixed the named capture issue (#1148)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(trie-router): fixed the named capture issue * denoify * denoify * refactor(pattern-router): Use Object.assign for shortening the code. (#1154) * refactor(pattern-router): Use `Object.assign` for shortening the code. * chore: denoify ---------

authored by Yusuke Wada · committed by GitHub · parent 0e1755c · Browse files · 7 files+23 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Taku Amano <taku@taaas.jp>

Diff stats

7 files+23 added−22 removed

  • tests:+5 added−4 removed
  • other:+18 added−18 removed

7 files+23 added−22 removed

modified:deno_dist/router/pattern-router/router.ts+8 added-5 removed
modified:deno_dist/router/trie-router/node.ts+1 added-4 removed
31 unmodified lines
32 patterns: Pattern[]33 order: number = 034 name: string35 handlerSetCache: Record<string, HandlerSet<T>[]>36 shouldCapture: boolean = false3637 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {38 this.children = children || {}39 this.methods = []
33 unmodified lines
7374 const pattern = getPattern(p)75 if (pattern) {76 if (typeof pattern === 'object') {78 this.shouldCapture = true77 for (let j = 0, len = parentPatterns.length; j < len; j++) {78 if (typeof parentPatterns[j] === 'object' && parentPatterns[j][1] === pattern[1]) {79 throw new Error(errorMessage(pattern[1]))80 }
6 unmodified lines
87 parentPatterns.push(...curNode.patterns)88 }89 parentPatterns.push(...curNode.patterns)90 curNode = curNode.children[p]93 curNode.shouldCapture = this.shouldCapture91 }9293 if (!curNode.methods.length) {94 curNode.methods = []
101 unmodified lines
196 // GET /book/b ~> no-slug-b, param['slug'] => b197 if (typeof name === 'string' && !matched) {198 params[name] = part199 } else {203 if (node.children[part] && node.children[part].shouldCapture) {200 if (node.children[part]) {201 params[name] = part202 }203 }204 }
21 unmodified lines
modified:src/router/pattern-router/router.test.ts+3 added-2 removed
155 unmodified lines
156 router.add('GET', '/book/b', 'no-slug-b')157 it('GET /book/a', async () => {158 const res = router.match('GET', '/book/a')159 expect(res?.handlers).toEqual(['no-slug', 'slug'])160 expect(res?.params['slug']).toBeUndefined()160 expect(res?.params['slug']).toBe('a')161 })162 it('GET /book/foo', async () => {163 const res = router.match('GET', '/book/foo')164 expect(res?.handlers).toEqual(['slug'])
268 unmodified lines
433 it('/book/a', () => {434 const res = router.match('get', '/book/a')435 expect(res).not.toBeNull()436 expect(res?.handlers).toEqual(['no-slug', 'slug'])437 expect(res?.params['slug']).toBeUndefined()437 expect(res?.params['slug']).toBe('a')438 })439440 it('/book/foo', () => {441 const res = router.match('get', '/book/foo')442 expect(res).not.toBeNull()443 expect(res?.handlers).toEqual(['slug'])
29 unmodified lines
modified:src/router/pattern-router/router.ts+8 added-5 removed
modified:src/router/trie-router/node.test.ts+1 added-1 removed
602 unmodified lines
603 it('/book/a', () => {604 const res = node.search('get', '/book/a')605 expect(res).not.toBeNull()606 expect(res?.handlers).toEqual(['no-slug', 'slug'])607 expect(res?.params['slug']).toBeUndefined()607 expect(res?.params['slug']).toBe('a')608 })609 it('/book/foo', () => {610 const res = node.search('get', '/book/foo')611 expect(res).not.toBeNull()
10 unmodified lines
modified:src/router/trie-router/node.ts+1 added-4 removed
31 unmodified lines
32 patterns: Pattern[]33 order: number = 034 name: string35 handlerSetCache: Record<string, HandlerSet<T>[]>36 shouldCapture: boolean = false3637 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {38 this.children = children || {}39 this.methods = []
33 unmodified lines
7374 const pattern = getPattern(p)75 if (pattern) {76 if (typeof pattern === 'object') {78 this.shouldCapture = true77 for (let j = 0, len = parentPatterns.length; j < len; j++) {78 if (typeof parentPatterns[j] === 'object' && parentPatterns[j][1] === pattern[1]) {79 throw new Error(errorMessage(pattern[1]))80 }
6 unmodified lines
87 parentPatterns.push(...curNode.patterns)88 }89 parentPatterns.push(...curNode.patterns)90 curNode = curNode.children[p]93 curNode.shouldCapture = this.shouldCapture91 }9293 if (!curNode.methods.length) {94 curNode.methods = []
101 unmodified lines
196 // GET /book/b ~> no-slug-b, param['slug'] => b197 if (typeof name === 'string' && !matched) {198 params[name] = part199 } else {203 if (node.children[part] && node.children[part].shouldCapture) {200 if (node.children[part]) {201 params[name] = part202 }203 }204 }
21 unmodified lines
modified:src/router/trie-router/router.test.ts+1 added-1 removed
156 unmodified lines
157 router.add('GET', '/book/b', 'no-slug-b')158 it('GET /book/a', async () => {159 const res = router.match('GET', '/book/a')160 expect(res?.handlers).toEqual(['no-slug', 'slug'])161 expect(res?.params['slug']).toBeUndefined()161 expect(res?.params['slug']).toBe('a')162 })163 it('GET /book/foo', async () => {164 const res = router.match('GET', '/book/foo')165 expect(res?.handlers).toEqual(['slug'])
28 unmodified lines