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
Provenance
- Commit
2d6abaf- Tree
9f75f04- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
0e1755c
7 files changed+23 added−22 removed
modified:deno_dist/router/pattern-router/router.ts+8 added-5 removed
12344567838 unmodified lines47484950515152535455565758575859606162636465666768666970717273import type { Result, Router } from '../../router.ts'import { METHOD_NAME_ALL } from '../../router.ts'
type Route<T> = [RegExp, string, T] // [pattern, method, handler]type Route<T> = [RegExp, string, T] // [pattern, method, handler, path]
export class PatternRouter<T> implements Router<T> { name: string = 'PatternRouter' private routes: Route<T>[] = []38 unmodified lines }
match(method: string, path: string): Result<T> | null { const handlers: T[] = [] let params: Record<string, string> | undefined = undefined const params: Record<string, string> = {}
for (const [pattern, routeMethod, handler] of this.routes) { const isRegExp = pattern.source.charCodeAt(pattern.source.length - 1) === 36 if (routeMethod === METHOD_NAME_ALL || routeMethod === method) { const match = pattern.exec(path) if (match) { handlers.push(handler) if (pattern.source.charCodeAt(pattern.source.length - 1) === 36) { params ??= match.groups || {} if (isRegExp) { Object.assign(params, match.groups) } } } }
return handlers.length ? { handlers, params: params || {}, params, } : null }}modified:deno_dist/router/trie-router/node.ts+1 added-4 removed
31 unmodified lines32 patterns: Pattern[]33 order: number = 034 name: string35 handlerSetCache: Record<string, HandlerSet<T>[]>36- shouldCapture: boolean = false36 37 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {38 this.children = children || {}39 this.methods = []33 unmodified lines73 74 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 lines87 parentPatterns.push(...curNode.patterns)88 }89 parentPatterns.push(...curNode.patterns)90 curNode = curNode.children[p]93- curNode.shouldCapture = this.shouldCapture91 }92 93 if (!curNode.methods.length) {94 curNode.methods = []101 unmodified lines196 // 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 linesmodified:src/router/pattern-router/router.test.ts+3 added-2 removed
155 unmodified lines156 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 lines433 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 })439+440 it('/book/foo', () => {441 const res = router.match('get', '/book/foo')442 expect(res).not.toBeNull()443 expect(res?.handlers).toEqual(['slug'])29 unmodified linesmodified:src/router/pattern-router/router.ts+8 added-5 removed
12344567838 unmodified lines47484950515152535455565758575859606162636465666768666970717273import type { Result, Router } from '../../router'import { METHOD_NAME_ALL } from '../../router'
type Route<T> = [RegExp, string, T] // [pattern, method, handler]type Route<T> = [RegExp, string, T] // [pattern, method, handler, path]
export class PatternRouter<T> implements Router<T> { name: string = 'PatternRouter' private routes: Route<T>[] = []38 unmodified lines }
match(method: string, path: string): Result<T> | null { const handlers: T[] = [] let params: Record<string, string> | undefined = undefined const params: Record<string, string> = {}
for (const [pattern, routeMethod, handler] of this.routes) { const isRegExp = pattern.source.charCodeAt(pattern.source.length - 1) === 36 if (routeMethod === METHOD_NAME_ALL || routeMethod === method) { const match = pattern.exec(path) if (match) { handlers.push(handler) if (pattern.source.charCodeAt(pattern.source.length - 1) === 36) { params ??= match.groups || {} if (isRegExp) { Object.assign(params, match.groups) } } } }
return handlers.length ? { handlers, params: params || {}, params, } : null }}modified:src/router/trie-router/node.test.ts+1 added-1 removed
602 unmodified lines603 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 linesmodified:src/router/trie-router/node.ts+1 added-4 removed
31 unmodified lines32 patterns: Pattern[]33 order: number = 034 name: string35 handlerSetCache: Record<string, HandlerSet<T>[]>36- shouldCapture: boolean = false36 37 constructor(method?: string, handler?: T, children?: Record<string, Node<T>>) {38 this.children = children || {}39 this.methods = []33 unmodified lines73 74 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 lines87 parentPatterns.push(...curNode.patterns)88 }89 parentPatterns.push(...curNode.patterns)90 curNode = curNode.children[p]93- curNode.shouldCapture = this.shouldCapture91 }92 93 if (!curNode.methods.length) {94 curNode.methods = []101 unmodified lines196 // 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 linesmodified:src/router/trie-router/router.test.ts+1 added-1 removed
156 unmodified lines157 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