Skip to content
Myr

Pass the tests

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

authored by Yusuke Wada · committed by Yusuke Wada · parent 265a90e · Browse files · 6 files+149 added−73 removed
This commit

State

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

Diff stats

6 files+149 added−73 removed

  • tests:+38 added−17 removed
  • other:+111 added−56 removed

6 files+149 added−73 removed

modified:example/basic/index.js+43 added
modified:src/hono.js+16 added-5 removed
modified:src/node.js+25 added-51 removed
1const { splitPath, getPattern, getParamName } = require('./util')23const METHOD_NAME_OF_ALL = 'all'45const createResult = (handler, params) => {6 return { handler: handler, params: params }
10 unmodified lines
17 }1819 insert(method, path, handler) {20 let curNode = this19 for (const p of this.splitPath(path)) {20 let nextNode = curNode.children[p]21 for (const p of splitPath(path)) {22 const nextNode = curNode.children[p]23 if (nextNode) {24 curNode = nextNode23 } else {24 curNode.children[p] = new Node({25 label: p,26 handler: handler,27 })28 curNode = curNode.children[p]25 continue26 }27 curNode.children[p] = new Node({28 label: p,29 method: method,30 handler: handler,31 })32 curNode = curNode.children[p]33 }34 curNode.method[method] = handler35 return curNode36 }3734 splitPath(path) {35 return ['/', ...path.split('/').filter((p) => p !== '')]36 }3738 getPattern(label) {39 // :id{[0-9]+} → [0-9]+$40 // :id → (.+)41 const match = label.match(/^\:.+?\{(.+)\}$/)42 if (match) {43 return '(' + match[1] + ')'44 }45 return '(.+)'46 }4748 getParamName(label) {49 const match = label.match(/^\:([^\{\}]+)/)50 if (match) {51 return match[1]52 }53 }5438 search(method, path) {39 let curNode = this57 let handler58 let params = {}40 const params = {}41 const parts = splitPath(path)4260 if (path === '/') {61 const root = this.children['/']62 if (!root) return this.noRoute()63 // app.get('*', () => 'All')64 const rootAsterisk = root.children['*']65 if (rootAsterisk) {66 handler =67 rootAsterisk.method[method] || rootAsterisk.method[METHOD_NAME_OF_ALL]68 } else if (!root.method[method]) {69 return this.noRoute()70 }71 }43 for (const [i, p] of parts.entries()) {44 const nextNode = curNode.children[p]4573 for (const p of this.splitPath(path)) {74 let nextNode = curNode.children[p]7546 if (nextNode) {47 curNode = nextNode48 continue49 }5051 let isParamMatch = false52 for (const key in curNode.children) {53 // Wildcard54 if (key === '*') {84 // Wildcard55 curNode = curNode.children[key]56 isParamMatch = true57 break58 } else if (key.match(/^:/)) {89 const pattern = this.getPattern(key)59 const pattern = getPattern(key)60 const match = p.match(new RegExp(pattern))61 if (match) {92 const k = this.getParamName(key)62 const k = getParamName(key)63 params[k] = match[0]64 curNode = curNode.children[key]65 isParamMatch = true66 break67 }68 return this.noRoute()69 }70 }7172 if (isParamMatch == false) {73 return this.noRoute()74 }7576 if (i === parts.length - 1) {77 break78 }79 }80106 handler =107 handler || curNode.method[METHOD_NAME_OF_ALL] || curNode.method[method]81 const handler = curNode.method[METHOD_NAME_OF_ALL] || curNode.method[method]8283 if (!handler) {84 return this.noRoute()85 }
10 unmodified lines
modified:src/node.test.js+8 added-17 removed
1const Node = require('./node')23describe('Util Methods', () => {4 const node = new Node()5 it('node.splitPath', () => {6 let ps = node.splitPath('/')7 expect(ps[0]).toBe('/')8 ps = node.splitPath('/hello')9 expect(ps[0]).toBe('/')10 expect(ps[1]).toBe('hello')11 })12})133describe('Root Node', () => {4 const node = new Node()5 node.insert('get', '/', 'get root')6 it('get /', () => {18 expect(node.search('get', '/')).not.toBeNull()7 let res = node.search('get', '/')8 expect(res).not.toBeNull()9 expect(res.handler).toBe('get root')10 expect(node.search('get', '/hello')).toBeNull()11 })12})1323describe('Root Node', () => {14describe('Root Node id not defined', () => {15 const node = new Node()16 node.insert('get', '/hello', 'get hello')17 it('get /', () => {18 expect(node.search('get', '/')).toBeNull()19 })20})2131describe('All', () => {22describe('All with *', () => {23 const node = new Node()24 node.insert('get', '*', 'get all')25 it('get /', () => {26 expect(node.search('get', '/')).not.toBeNull()
41 unmodified lines
68 })6970 it('get /entry/456/comment', () => {71 node.insert('get', '/entry/:id', 'get entry')81 res = node.search('get', '/entry/456/comment')72 let res = node.search('get', '/entry/456/comment')73 expect(res).toBeNull()74 })7576 it('get /entry/789/comment/123', () => {77 node.insert('get', '/entry/:id/comment/:comment_id', 'get comment')87 res = node.search('get', '/entry/789/comment/123')78 let res = node.search('get', '/entry/789/comment/123')79 expect(res).not.toBeNull()80 expect(res.handler).toBe('get comment')81 expect(res.params['id']).toBe('789')82 expect(res.params['comment_id']).toBe('123')
3 unmodified lines
86describe('Wildcard', () => {87 const node = new Node()88 it('/wildcard-abc/xxxxxx/wildcard-efg', () => {89 node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard')99 res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg')90 let res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg')91 expect(res).not.toBeNull()92 expect(res.handler).toBe('wildcard')93 })94})
37 unmodified lines
added:src/util.js+27 added
added:src/util.test.js+30 added
1const { splitPath, getPattern, getParamName } = require('./util')23describe('Utility methods', () => {4 it('splitPath', () => {5 let ps = splitPath('/')6 expect(ps[0]).toBe('')7 ps = splitPath('/hello')8 expect(ps[0]).toBe('hello')9 ps = splitPath('*')10 expect(ps[0]).toBe('*')11 ps = splitPath('/wildcard-abc/*/wildcard-efg')12 expect(ps[0]).toBe('wildcard-abc')13 expect(ps[1]).toBe('*')14 expect(ps[2]).toBe('wildcard-efg')15 })1617 it('getPattern', () => {18 let res = getPattern(':id')19 expect(res).toBe('(.+)')20 res = getPattern(':id{[0-9]+}')21 expect(res).toBe('([0-9]+)')22 })2324 it('getParamName', () => {25 let res = getParamName(':id')26 expect(res).toBe('id')27 res = getParamName(':id{[0-9]+}')28 expect(res).toBe('id')29 })30})