Skip to content
Myr

Update router!

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 5777f2c · Browse files · 6 files+319 added−239 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+319 added−239 removed

  • tests:+170 added−73 removed
  • other:+149 added−166 removed

6 files+319 added−239 removed

modified:example/basic/index.js+1 added-37 removed
added:src/node.js+119 added
1const methodNameOfAll = 'all'23class Result {4 constructor({ handler, params } = {}) {5 this.handler = handler6 this.params = params || {}7 }8}910class Node {11 constructor({ method, label, handler, children } = {}) {12 this.label = label || '/'13 this.children = children || []14 this.method = {}15 if (method && handler) {16 this.method[method] = handler17 }18 }1920 insert(method, path, handler) {21 let curNode = this22 const ps = this.splitPath(path)23 for (const p of ps) {24 let nextNode = curNode.children[p]25 if (nextNode) {26 curNode = nextNode27 } else {28 curNode.children[p] = new Node({29 method: method,30 label: p,31 handler: handler,32 })33 curNode = curNode.children[p]34 }35 }36 // XXX37 if (!curNode.method[method]) {38 curNode.method[method] = handler39 }40 }4142 splitPath(path) {43 let ps = ['/']44 for (const p of path.split('/')) {45 if (p) {46 ps.push(p)47 }48 }49 return ps50 }5152 getPattern(label) {53 // :id{[0-9]+} → [0-9]+$54 // :id → (.+)55 const match = label.match(/^\:.+?\{(.+)\}$/)56 if (match) {57 return '(' + match[1] + ')'58 }59 return '(.+)'60 }6162 getParamName(label) {63 const match = label.match(/^\:([^\{\}]+)/)64 if (match) {65 return match[1]66 }67 }6869 search(method, path) {70 let curNode = this71 const params = {}72 for (const p of this.splitPath(path)) {73 let nextNode = curNode.children[p]74 if (nextNode) {75 curNode = nextNode76 continue77 }78 let isParamMatch = false79 for (const key in curNode.children) {80 if (key === '*') {81 // Wildcard82 curNode = curNode.children[key]83 isParamMatch = true84 break85 }86 if (key.match(/^:/)) {87 const pattern = this.getPattern(key)88 const match = p.match(new RegExp(pattern))89 if (match) {90 const k = this.getParamName(key)91 params[k] = match[0]92 curNode = curNode.children[key]93 isParamMatch = true94 break95 }96 return this.noRoute()97 }98 }99 if (isParamMatch == false) {100 return this.noRoute()101 }102 }103104 let handler = curNode.method[methodNameOfAll] || curNode.method[method]105106 if (handler) {107 const res = new Result({ handler: handler, params: params })108 return res109 } else {110 return this.noRoute()111 }112 }113114 noRoute() {115 return null116 }117}118119module.exports = Node
added:src/node.test.js+110 added
1const Node = require('./node')2const node = new Node()34describe('Util Methods', () => {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})1314describe('Basic Usage', () => {15 node.insert('get', '/', 'get root')16 node.insert('get', '/hello', 'get hello')17 node.insert('post', '/hello', 'post hello')18 node.insert('get', '/hello/foo', 'get hello foo')1920 it('get, post /hello', () => {21 expect(node.search('get', '/').handler).toBe('get root')22 expect(node.search('post', '/')).toBeNull()2324 expect(node.search('get', '/hello').handler).toBe('get hello')25 expect(node.search('post', '/hello').handler).toBe('post hello')26 expect(node.search('put', '/hello')).toBeNull()27 })28 it('get /nothing', () => {29 expect(node.search('get', '/nothing')).toBeNull()30 })31 it('/hello/foo, /hello/bar', () => {32 expect(node.search('get', '/hello/foo').handler).toBe('get hello foo')33 expect(node.search('post', '/hello/foo')).toBeNull()34 expect(node.search('get', '/hello/bar')).toBeNull()35 })36 it('/hello/foo/bar', () => {37 expect(node.search('get', '/hello/foo/bar')).toBeNull()38 })39})4041describe('Name path', () => {42 it('get /entry/123', () => {43 node.insert('get', '/entry/:id', 'get entry')44 let res = node.search('get', '/entry/123')45 expect(res).not.toBeNull()46 expect(res.handler).toBe('get entry')47 expect(res.params).not.toBeNull()48 expect(res.params['id']).toBe('123')49 expect(res.params['id']).not.toBe('1234')50 })5152 it('get /entry/456/comment', () => {53 node.insert('get', '/entry/:id', 'get entry')54 res = node.search('get', '/entry/456/comment')55 expect(res).toBeNull()56 })5758 it('get /entry/789/comment/123', () => {59 node.insert('get', '/entry/:id/comment/:comment_id', 'get comment')60 res = node.search('get', '/entry/789/comment/123')61 expect(res).not.toBeNull()62 expect(res.handler).toBe('get comment')63 expect(res.params['id']).toBe('789')64 expect(res.params['comment_id']).toBe('123')65 })66})6768describe('Wildcard', () => {69 it('/wildcard-abc/xxxxxx/wildcard-efg', () => {70 node.insert('get', '/wildcard-abc/*/wildcard-efg', 'wildcard')71 res = node.search('get', '/wildcard-abc/xxxxxx/wildcard-efg')72 expect(res).not.toBeNull()73 expect(res.handler).toBe('wildcard')74 })75})7677describe('Regexp', () => {78 node.insert(79 'get',80 '/regex-abc/:id{[0-9]+}/comment/:comment_id{[a-z]+}',81 'regexp'82 )83 it('/regexp-abc/123/comment/abc', () => {84 res = node.search('get', '/regex-abc/123/comment/abc')85 expect(res).not.toBeNull()86 expect(res.handler).toBe('regexp')87 expect(res.params['id']).toBe('123')88 expect(res.params['comment_id']).toBe('abc')89 })90 it('/regexp-abc/abc', () => {91 res = node.search('get', '/regex-abc/abc')92 expect(res).toBeNull()93 })94 it('/regexp-abc/123/comment/123', () => {95 res = node.search('get', '/regex-abc/123/comment/123')96 expect(res).toBeNull()97 })98})99100describe('All', () => {101 node.insert('all', '/all-methods', 'all methods')102 it('/all-methods', () => {103 res = node.search('get', '/all-methods')104 expect(res).not.toBeNull()105 expect(res.handler).toBe('all methods')106 res = node.search('put', '/all-methods')107 expect(res).not.toBeNull()108 expect(res.handler).toBe('all methods')109 })110})
modified:src/router.js+28 added-128 removed
modified:src/router.test.js+60 added-73 removed
modified:package.json+1 added-1 removed
19 unmodified lines
20 "dependencies": {21 "global": "^4.4.0",22 "wrangler": "^0.0.0-beta.6"23 }24}24}