Skip to content
Myr

refactor(trie-router): routes are returned in the registration order (#325)

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

authored by Yusuke Wada · committed by GitHub · parent 3179adc · Browse files · 2 files+13 added−26 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+13 added−26 removed

  • tests:+12 added−14 removed
  • other:+1 added−12 removed

2 files+13 added−26 removed

modified:src/router/trie-router/node.test.ts+12 added-14 removed
415 unmodified lines
416417 it('get /api/posts/123', () => {418 const res = node.search('get', '/api/posts/123')419 expect(res).not.toBeNull()420 expect(res?.handlers).toEqual(['1st', '2nd', '4th', '3rd'])420 expect(res?.handlers).toEqual(['1st', '2nd', '3rd', '4th'])421 })422 })423424 describe('With special Wildcard', () => {
11 unmodified lines
436 })437438 describe('Complex', () => {439 const node = new Node()440 node.insert('get', '/api', 'x') // score 1.1441 node.insert('get', '/api/*', 'c') // score 2.2442 node.insert('get', '/api/:type', 'y') // score 2.3443 node.insert('get', '/api/:type/:id', 'd') // score 3.4444 node.insert('get', '/api/posts/:id', 'e') // score 3.5445 node.insert('get', '/api/posts/123', 'f') // score 3.6446 node.insert('get', '/*/*/:id', 'g') // score 3.7447 node.insert('get', '/api/posts/*/comment', 'z') // score 4.8 - not match448 node.insert('get', '*', 'a') // score 1.9449 node.insert('get', '*', 'b') // score 1.10440 node.insert('get', '/api', 'a') // not match441 node.insert('get', '/api/*', 'b') // match442 node.insert('get', '/api/:type', 'c') // not match443 node.insert('get', '/api/:type/:id', 'd') // match444 node.insert('get', '/api/posts/:id', 'e') // match445 node.insert('get', '/api/posts/123', 'f') // match446 node.insert('get', '/*/*/:id', 'g') // match447 node.insert('get', '/api/posts/*/comment', 'h') // not match448 node.insert('get', '*', 'i') // match449 node.insert('get', '*', 'j') // match450451 it('get /api/posts/123', () => {452 const res = node.search('get', '/api/posts/123')453 // ---> will match => c, d, e, e, f, g, a, b454 // ---> sort by score => a, b, c, d, e, f, g455 expect(res?.handlers).toEqual(['a', 'b', 'c', 'd', 'e', 'f', 'g'])453 expect(res?.handlers).toEqual(['b', 'd', 'e', 'f', 'g', 'i', 'j'])454 })455 })456457 describe('Multi match', () => {
72 unmodified lines
modified:src/router/trie-router/node.ts+1 added-12 removed
89 unmodified lines
90 parentPatterns.push(...curNode.patterns)91 curNode = curNode.children[p]92 }9394 let score = 19596 if (path === '*') {97 score = score + this.order * 0.0198 } else {99 score = parts.length + this.order * 0.01100 }10194 if (!curNode.methods.length) {95 curNode.methods = []96 }9798 const m: Record<string, HandlerSet<T>> = {}99108 const handlerSet: HandlerSet<T> = { handler: handler, name: this.name, score: score }100 const handlerSet: HandlerSet<T> = { handler: handler, name: this.name, score: this.order }101102 m[method] = handlerSet103 curNode.methods.push(m)104
6 unmodified lines
111 node.methods.map((m) => {112 const handlerSet = m[method] || m[METHOD_NAME_ALL]113 if (handlerSet !== undefined) {114 const hs = { ...handlerSet }123 if (wildcard) {124 hs.score = handlerSet.score - 1125 }115 handlerSets.push(hs)116 return117 }118 })
84 unmodified lines