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
Provenance
- Commit
6a7746d- Tree
f9ca1a7- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
3179adc
2 files changed+13 added−26 removed
modified:src/router/trie-router/node.test.ts+12 added-14 removed
415 unmodified lines416 417 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 })423 424 describe('With special Wildcard', () => {11 unmodified lines436 })437 438 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') // match450 451 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 })456 457 describe('Multi match', () => {72 unmodified linesmodified:src/router/trie-router/node.ts+1 added-12 removed
89 unmodified lines90 parentPatterns.push(...curNode.patterns)91 curNode = curNode.children[p]92 }93 94- let score = 195-96- if (path === '*') {97- score = score + this.order * 0.0198- } else {99- score = parts.length + this.order * 0.01100- }101-94 if (!curNode.methods.length) {95 curNode.methods = []96 }97 98 const m: Record<string, HandlerSet<T>> = {}99 108- const handlerSet: HandlerSet<T> = { handler: handler, name: this.name, score: score }100+ const handlerSet: HandlerSet<T> = { handler: handler, name: this.name, score: this.order }101 102 m[method] = handlerSet103 curNode.methods.push(m)104 6 unmodified lines111 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