Skip to content
Myr

fix(trie-router-router): For "Named param with trailing wildcard" (#1246)

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

* fix(trie-router-router): For "Named param with trailing wildcard" * denoify

authored by Yusuke Wada · committed by GitHub · parent 3e663af · Browse files · 4 files+56 added−6 removed
This commit

State

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

Diff stats

4 files+56 added−6 removed

  • tests:+38 added−0 removed
  • other:+18 added−6 removed

4 files+56 added−6 removed

modified:deno_dist/router/trie-router/node.ts+9 added-3 removed
169 unmodified lines
170 // Named match171 // `/posts/:id` => match /posts/123172 const [key, name, matcher] = pattern173174 const child = node.children[key]175176 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js177 const restPathString = parts.slice(i).join('/')178 if (matcher instanceof RegExp && matcher.test(restPathString)) {177 handlerSets.push(...this.gHSets(node.children[key], method))179 handlerSets.push(...this.gHSets(child, method))180 params[name] = restPathString181 continue182 }183184 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {185 if (typeof key === 'string') {186 if (isLast === true) {185 handlerSets.push(...this.gHSets(node.children[key], method))187 handlerSets.push(...this.gHSets(child, method))188 // `/:id/*` => match `/foo`189 if (child.children['*']) {190 handlerSets.push(...this.gHSets(child.children['*'], method))191 }192 } else {187 tempNodes.push(node.children[key])193 tempNodes.push(child)194 }195 }196197 // `/book/a` => no-slug
34 unmodified lines
modified:src/router/trie-router/node.test.ts+25 added
125 unmodified lines
126 expect(res?.params['baz']).toBe('world')127 })128})129130describe('Named params and a wildcard', () => {131 const node = new Node()132133 node.insert('get', '/:id/*', 'onepart')134135 it('get /', () => {136 const res = node.search('get', '/')137 expect(res).toBeNull()138 })139140 it('get /foo', () => {141 const res = node.search('get', '/foo')142 expect(res).not.toBeNull()143 expect(res?.handlers).toEqual(['onepart'])144 expect(res?.params['id']).toEqual('foo')145 })146147 it('get /foo/bar', () => {148 const res = node.search('get', '/foo/bar')149 expect(res).not.toBeNull()150 expect(res?.handlers).toEqual(['onepart'])151 expect(res?.params['id']).toEqual('foo')152 })153})154155describe('Wildcard', () => {156 const node = new Node()157 node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard')158 it('/wildcard-abc/xxxxxx/wildcard-efg', () => {
488 unmodified lines
modified:src/router/trie-router/node.ts+9 added-3 removed
169 unmodified lines
170 // Named match171 // `/posts/:id` => match /posts/123172 const [key, name, matcher] = pattern173174 const child = node.children[key]175176 // `/js/:filename{[a-z]+.js}` => match /js/chunk/123.js177 const restPathString = parts.slice(i).join('/')178 if (matcher instanceof RegExp && matcher.test(restPathString)) {177 handlerSets.push(...this.gHSets(node.children[key], method))179 handlerSets.push(...this.gHSets(child, method))180 params[name] = restPathString181 continue182 }183184 if (matcher === true || (matcher instanceof RegExp && matcher.test(part))) {185 if (typeof key === 'string') {186 if (isLast === true) {185 handlerSets.push(...this.gHSets(node.children[key], method))187 handlerSets.push(...this.gHSets(child, method))188 // `/:id/*` => match `/foo`189 if (child.children['*']) {190 handlerSets.push(...this.gHSets(child.children['*'], method))191 }192 } else {187 tempNodes.push(node.children[key])193 tempNodes.push(child)194 }195 }196197 // `/book/a` => no-slug
34 unmodified lines
modified:src/router/trie-router/router.test.ts+13 added
37 unmodified lines
38 expect(res?.handlers).toEqual(['get entry'])39 expect(res?.params['id']).toBe('123')40 })4142 it('Named param with trailing wildcard', async () => {43 router.add('GET', '/article/:id/*', 'get article with wildcard')44 let res = router.match('GET', '/article/123')45 expect(res).not.toBeNull()46 expect(res?.handlers).toEqual(['get article with wildcard'])47 expect(res?.params['id']).toBe('123')4849 res = router.match('GET', '/article/123/action')50 expect(res).not.toBeNull()51 expect(res?.handlers).toEqual(['get article with wildcard'])52 expect(res?.params['id']).toBe('123')53 })5455 it('Wildcard', async () => {56 router.add('GET', '/wild/*/card', 'get wildcard')57 const res = router.match('GET', '/wild/xxx/card')58 expect(res).not.toBeNull()
148 unmodified lines