Skip to content
Myr

refactor(reg-exp-router): Routes are returned in the registration order. (#324)

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

authored by Taku Amano · committed by GitHub · parent f6c9592 · Browse files · 2 files+17 added−7 removed
This commit

State

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

Diff stats

2 files+17 added−7 removed

  • tests:+16 added−0 removed
  • other:+1 added−7 removed

2 files+17 added−7 removed

modified:src/router/reg-exp-router/router.test.ts+16 added
104 unmodified lines
105 const res = router.match('GET', '/posts/123')106 expect(res).not.toBeNull()107 expect(res?.handlers).toEqual(['bar', 'foo'])108 })109110 it('middleware -> handler', async () => {111 router.add('GET', '*', 'bar')112 router.add('GET', '/:type/:action', 'foo')113 const res = router.match('GET', '/posts/123')114 expect(res).not.toBeNull()115 expect(res?.handlers).toEqual(['bar', 'foo'])116 })117118 it('handler -> fallback', async () => {119 router.add('GET', '/:type/:action', 'foo')120 router.add('GET', '*', 'fallback')121 const res = router.match('GET', '/posts/123')122 expect(res).not.toBeNull()123 expect(res?.handlers).toEqual(['foo', 'fallback'])124 })125})126127describe('Optimization for METHOD_NAME_OF_ALL', () => {128 let router: RegExpRouter<string>
167 unmodified lines
modified:src/router/reg-exp-router/router.ts+1 added-7 removed
14 unmodified lines
15}16interface HandlerWithSortIndex<T> {17 handler: T18 index: number19 componentsLength: number19}20interface Route<T> {21 method: string22 path: string
85 unmodified lines
108 return i === b.hint.regExpComponents.length || a.hint.endWithWildcard ? 1 : 0109}110111function compareHandler(a: HandlerWithSortIndex<any>, b: HandlerWithSortIndex<any>) {113 return a.componentsLength !== b.componentsLength114 ? a.componentsLength - b.componentsLength115 : a.index - b.index112 return a.index - b.index113}114115function getSortedHandlers<T>(116 handlers: HandlerWithSortIndex<T>[] | IterableIterator<HandlerWithSortIndex<T>>
103 unmodified lines
220 const hint = initHint(path)221 const handlerWithSortIndex = {222 index,223 handler,227 componentsLength: hint.components.length || 1,224 }225226 for (let i = 0, len = routes.length; i < len; i++) {227 if (routes[i].method === method && routes[i].path === path) {
183 unmodified lines
411 }412 }413414 if (routes[j].hint.components.length < routes[i].hint.components.length) {419 const componentsLength = routes[j].hint.components.length || 1415 routes[j].middleware.push(416 ...routes[i].handlers.map((h) => ({422 componentsLength,417 index: h.index,418 handler: h.handler,419 }))420 )
40 unmodified lines