From 023b772c87010febc792a80fb8d6e49c52d23330 Mon Sep 17 00:00:00 2001 From: fundon Date: Mon, 19 Oct 2015 21:38:23 +0800 Subject: [PATCH] 0.5.1 --- lib/Router.js | 4 +- package.json | 14 +++- src/Router.js | 220 +++++++++++++++++++++++++------------------------- 3 files changed, 124 insertions(+), 114 deletions(-) diff --git a/lib/Router.js b/lib/Router.js index d9488e3..79bcb09 100644 --- a/lib/Router.js +++ b/lib/Router.js @@ -87,7 +87,7 @@ var Router = (function () { value: new Node('/') }); Object.defineProperty(_this, m, { - get: function get() { + get() { return function verb(path, handler) { return this.add(m.toUpperCase(), path, handler); }; @@ -319,7 +319,7 @@ var Router = (function () { function lcp(a, b) { var i = 0; - var max = min(a.length, b.length); + const max = min(a.length, b.length); for (; i < max && a.charCodeAt(i) === b.charCodeAt(i); ++i) {} return i; } diff --git a/package.json b/package.json index 27b692b..c7c134b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "trek-router", - "version": "0.5.0", + "version": "0.5.1", "description": "A fast HTTP router", "repository": "trekjs/router", "main": "lib/Router.js", @@ -26,7 +26,7 @@ "benchmark": "^1.0.0", "finalhandler": "^0.4.0", "isparta": "^3.x", - "istanbul": "^0.3.x", + "istanbul": "^0.4.0", "lodash": "^3.10.1", "mocha": "^2.3.3", "path-to-regexp": "^1.2.1", @@ -46,5 +46,15 @@ }, "dependencies": { "methods": "^1.1.1" + }, + "babel": { + "blacklist": [ + "regenerator", + "es6.constants", + "es6.properties.computed", + "es6.properties.shorthand", + "es6.tailCall", + "es6.templateLiterals" + ] } } diff --git a/src/Router.js b/src/Router.js index 3c7e919..2005411 100644 --- a/src/Router.js +++ b/src/Router.js @@ -4,13 +4,13 @@ * MIT Licensed */ -import METHODS from 'methods'; +import METHODS from 'methods' -const min = Math.min; +const min = Math.min -const STAR = 42; // '*' -const SLASH = 47; // '/' -const COLON = 58; // ':' +const STAR = 42 // '*' +const SLASH = 47 // '/' +const COLON = 58 // ':' /** * Route Node @@ -25,11 +25,11 @@ const COLON = 58; // ':' class Node { constructor(prefix, children, handler, pnames) { - this.label = prefix.charCodeAt(0); - this.prefix = prefix; - this.children = children || []; - this.handler = handler; - this.pnames = pnames; + this.label = prefix.charCodeAt(0) + this.prefix = prefix + this.children = children || [] + this.handler = handler + this.pnames = pnames } /** @@ -39,13 +39,13 @@ class Node { * @return {Node|undefined} node */ findChild(c) { - let [i, l, e] = [0, this.children.length, undefined]; + let [i, l, e] = [0, this.children.length, undefined] for (; i < l; ++i) { - e = this.children[i]; + e = this.children[i] // Compare charCode - if (e.label === c) return e; + if (e.label === c) return e } - return undefined; + return undefined } } @@ -59,20 +59,20 @@ class Node { class Router { constructor() { - this.trees = Object.create(null); + this.trees = Object.create(null) METHODS.forEach((m) => { // Start from '/' Object.defineProperty(this.trees, m.toUpperCase(), { value: new Node('/') - }); + }) Object.defineProperty(this, m, { get() { return function verb(path, handler) { - return this.add(m.toUpperCase(), path, handler); - }; + return this.add(m.toUpperCase(), path, handler) + } } - }); - }); + }) + }) } /** @@ -84,36 +84,36 @@ class Router { * @param {Function|GeneratorFunction} handler */ add(method, path, handler) { - let i = 0; + let i = 0 let l = path.length - let pnames = []; // Param names - let ch, j; + let pnames = [] // Param names + let ch, j for (; i < l; ++i) { - ch = path.charCodeAt(i); + ch = path.charCodeAt(i) if (ch === COLON) { - j = i + 1; + j = i + 1 - this.insert(method, path.substring(0, i)); + this.insert(method, path.substring(0, i)) for (; i < l && (path.charCodeAt(i) !== SLASH); ++i) {} - pnames.push(path.substring(j, i)); - path = path.substring(0, j) + path.substring(i); - i = j; - l = path.length; + pnames.push(path.substring(j, i)) + path = path.substring(0, j) + path.substring(i) + i = j + l = path.length if (i === l) { - this.insert(method, path.substring(0, i), handler, pnames); - return; + this.insert(method, path.substring(0, i), handler, pnames) + return } - this.insert(method, path.substring(0, i)); + this.insert(method, path.substring(0, i)) } else if (ch === STAR) { - this.insert(method, path.substring(0, i)); - this.insert(method, path.substring(0, l), handler, pnames); - return; + this.insert(method, path.substring(0, i)) + this.insert(method, path.substring(0, l), handler, pnames) + return } } - this.insert(method, path, handler, pnames); + this.insert(method, path, handler, pnames) } /** @@ -127,62 +127,62 @@ class Router { * @param {Array} [pnames] */ insert(method, path, handler, pnames) { - let cn = this.trees[method]; // Current node as root - let search = path; - let sl, pl, l, n, c; + let cn = this.trees[method] // Current node as root + let search = path + let sl, pl, l, n, c while (true) { - sl = search.length; - pl = cn.prefix.length; - l = lcp(search, cn.prefix); + sl = search.length + pl = cn.prefix.length + l = lcp(search, cn.prefix) if (l === 0) { // At root node - cn.label = search.charCodeAt(0); - cn.prefix = search; + cn.label = search.charCodeAt(0) + cn.prefix = search if (handler) { - cn.handler = handler; - cn.pnames = pnames; + cn.handler = handler + cn.pnames = pnames } } else if (l < pl) { // Split node - n = new Node(cn.prefix.substring(l), cn.children, cn.handler, cn.pnames); - cn.children = [n]; // Add to parent + n = new Node(cn.prefix.substring(l), cn.children, cn.handler, cn.pnames) + cn.children = [n] // Add to parent // Reset parent node - cn.label = cn.prefix.charCodeAt(0); - cn.prefix = cn.prefix.substring(0, l); - cn.handler = undefined; - cn.pnames = undefined; + cn.label = cn.prefix.charCodeAt(0) + cn.prefix = cn.prefix.substring(0, l) + cn.handler = undefined + cn.pnames = undefined if (l === sl) { // At parent node - cn.handler = handler; - cn.pnames = pnames; + cn.handler = handler + cn.pnames = pnames } else { // Create child node - n = new Node(search.substring(l), [], handler, pnames); - cn.children.push(n); + n = new Node(search.substring(l), [], handler, pnames) + cn.children.push(n) } } else if (l < sl) { - search = search.substring(l); - c = cn.findChild(search.charCodeAt(0)); + search = search.substring(l) + c = cn.findChild(search.charCodeAt(0)) if (c !== undefined) { // Go deeper - cn = c; - continue; + cn = c + continue } // Create child node - n = new Node(search, [], handler, pnames); - cn.children.push(n); + n = new Node(search, [], handler, pnames) + cn.children.push(n) } else { // Node already exists if (handler) { - cn.handler = handler; - cn.pnames = pnames; + cn.handler = handler + cn.pnames = pnames } } - return; + return } } @@ -197,101 +197,101 @@ class Router { * @property {Array} result[1] */ find(method, path, cn, n, result) { - cn = cn || this.trees[method]; // Current node as root - n = n || 0; // Param count - result = result || [undefined, []]; - let search = path; - let params = result[1]; // Params - let pl, l, leq, c; - let preSearch; // Pre search + cn = cn || this.trees[method] // Current node as root + n = n || 0 // Param count + result = result || [undefined, []] + let search = path + let params = result[1] // Params + let pl, l, leq, c + let preSearch // Pre search // Search order static > param > match-any if (search.length === 0 || search === cn.prefix) { // Found - result[0] = cn.handler; + result[0] = cn.handler if (cn.handler !== undefined) { - let pnames = cn.pnames; + let pnames = cn.pnames if (pnames !== undefined) { - let i = 0; - let l = pnames.length; + let i = 0 + let l = pnames.length for (; i < l; ++i) { - params[i].name = pnames[i]; + params[i].name = pnames[i] } } } - return result; + return result } - pl = cn.prefix.length; - l = lcp(search, cn.prefix); - leq = l === pl; + pl = cn.prefix.length + l = lcp(search, cn.prefix) + leq = l === pl if (leq) { - search = search.substring(l); + search = search.substring(l) } - preSearch = search; + preSearch = search // Static node - c = cn.findChild(search.charCodeAt(0)); + c = cn.findChild(search.charCodeAt(0)) if (c !== undefined) { - this.find(method, search, c, n, result); - if (result[0] !== undefined) return result; - search = preSearch; + this.find(method, search, c, n, result) + if (result[0] !== undefined) return result + search = preSearch } // Not found node if (!leq) { - return result; + return result } // Param node - c = cn.findChild(COLON); + c = cn.findChild(COLON) if (c !== undefined) { - l = search.length; + l = search.length for (var i = 0; i < l && (search.charCodeAt(i) !== SLASH); ++i) {} params[n] = { value: search.substring(0, i) - }; + } - n++; - preSearch = search; - search = search.substring(i); + n++ + preSearch = search + search = search.substring(i) - this.find(method, search, c, n, result); - if (result[0] !== undefined) return result; + this.find(method, search, c, n, result) + if (result[0] !== undefined) return result - n--; + n-- params.shift() - search = preSearch; + search = preSearch } // Match-any node - c = cn.findChild(STAR); + c = cn.findChild(STAR) if (c !== undefined) { params[n] = { name: '_*', value: search - }; - search = ''; // End search - this.find(method, search, c, n, result); + } + search = '' // End search + this.find(method, search, c, n, result) } - return result; + return result } } // Length of longest common prefix function lcp(a, b) { - let i = 0; - let max = min(a.length, b.length); + let i = 0 + const max = min(a.length, b.length) for (; i < max && (a.charCodeAt(i) === b.charCodeAt(i)); ++i) {} - return i; + return i } -Router.METHODS = METHODS; +Router.METHODS = METHODS -Router.Node = Node; +Router.Node = Node -export default Router; +export default Router