Skip to content
Myr

feat: default route with wildcard (#52)

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

* Default wildcard route * Update example * Add test

authored by Yusuke Wada · committed by GitHub · parent bde8355 · Browse files · 4 files+31 added−1 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+31 added−1 removed

  • tests:+27 added−0 removed
  • other:+4 added−1 removed

4 files+31 added−1 removed

modified:example/basic/index.js+2 added
modified:src/node.ts+2 added-1 removed
56 unmodified lines
57 for (let i = 0, len = parts.length; i < len; i++) {58 const p: string = parts[i]5960 // '*' => match any path61 // /api/* => default wildcard match61 if (curNode.children['*']) {62 if (curNode.children['*'] && !curNode.children[p]) {63 const astNode = curNode.children['*']64 if (Object.keys(astNode.children).length === 0) {65 curNode = astNode66 break
60 unmodified lines
modified:test/node.test.ts+16 added
173 unmodified lines
174 expect(res).not.toBeNull()175 expect(res.handler).toBe('match hello')176 })177})178179describe('Default with wildcard', () => {180 const node = new Node()181 node.insert('ALL', '/api/abc', 'match api')182 node.insert('ALL', '/api/*', 'fallback')183 it('/api/abc', () => {184 const res = node.search('get', '/api/abc')185 expect(res).not.toBeNull()186 expect(res.handler).toBe('match api')187 })188 it('/api/def', () => {189 const res = node.search('get', '/api/def')190 expect(res).not.toBeNull()191 expect(res.handler).toBe('fallback')192 })193})
modified:test/router.test.ts+11 added