trek-router
Advanced tools
Comparing version 0.1.6 to 0.1.7
@@ -28,6 +28,7 @@ 'use strict'; | ||
* @param {Function|GeneratorFunction} handler | ||
* @param {Array} [keys] | ||
*/ | ||
var Node = (function () { | ||
function Node(prefix, children, handler) { | ||
function Node(prefix, children, handler, keys) { | ||
_classCallCheck(this, Node); | ||
@@ -39,2 +40,3 @@ | ||
this.handler = handler; | ||
this.keys = keys; | ||
} | ||
@@ -95,8 +97,6 @@ | ||
Router.prototype.add = function add(method, path, handler) { | ||
var i = 0; | ||
var l = path.length; | ||
// Store param keys | ||
var keys = []; | ||
if (handler) handler.keys = keys; | ||
var i = 0; | ||
var l = path.length; | ||
var ch = undefined, | ||
@@ -123,3 +123,3 @@ j = undefined; | ||
if (i === l) { | ||
this.insert(method, path.substring(0, i), handler); | ||
this.insert(method, path.substring(0, i), handler, keys); | ||
return; | ||
@@ -130,6 +130,6 @@ } | ||
this.insert(method, path.substring(0, i)); | ||
this.insert(method, path.substring(0, l), handler); | ||
this.insert(method, path.substring(0, l), handler, keys); | ||
} | ||
} | ||
this.insert(method, path, handler); | ||
this.insert(method, path, handler, keys); | ||
}; | ||
@@ -147,3 +147,3 @@ | ||
Router.prototype.insert = function insert(method, path, handler) { | ||
Router.prototype.insert = function insert(method, path, handler, keys) { | ||
var cn = this.trees[method]; // Current node as root | ||
@@ -166,8 +166,7 @@ var search = path; | ||
cn.prefix = search; | ||
if (handler) { | ||
cn.handler = handler; | ||
} | ||
if (handler) cn.handler = handler; | ||
if (keys) cn.keys = keys; | ||
} else if (l < pl) { | ||
// Split node | ||
n = new Node(cn.prefix.substring(l), cn.children, cn.handler); | ||
n = new Node(cn.prefix.substring(l), cn.children, cn.handler, cn.keys); | ||
cn.children = [n]; // Add to parent | ||
@@ -179,9 +178,11 @@ | ||
cn.handler = undefined; | ||
cn.keys = undefined; | ||
if (l === sl) { | ||
// At parent node | ||
cn.handler = handler; | ||
if (handler) cn.handler = handler; | ||
if (keys) cn.keys = keys; | ||
} else { | ||
// Create child node | ||
n = new Node(search.substring(l), [], handler); | ||
n = new Node(search.substring(l), [], handler, keys); | ||
cn.children.push(n); | ||
@@ -198,9 +199,8 @@ } | ||
// Create child node | ||
n = new Node(search, [], handler); | ||
n = new Node(search, [], handler, keys); | ||
cn.children.push(n); | ||
} else { | ||
// Node already exists | ||
if (handler) { | ||
cn.handler = handler; | ||
} | ||
if (handler) cn.handler = handler; | ||
if (keys) cn.keys = keys; | ||
} | ||
@@ -226,5 +226,2 @@ return; | ||
result = result || [undefined, []]; | ||
// let n = 0; // Param count | ||
// let cn = this.trees[method]; // Current node as root | ||
// let result = [undefined, []]; | ||
var search = path; | ||
@@ -243,7 +240,9 @@ var params = result[1]; | ||
if (cn.handler !== undefined) { | ||
var keys = cn.handler.keys; | ||
var _i = 0, | ||
_l = keys.length; | ||
for (; _i < _l; ++_i) { | ||
params[_i].name = keys[_i]; | ||
var keys = cn.keys; | ||
if (keys !== undefined) { | ||
var _i = 0, | ||
_l = keys.length; | ||
for (; _i < _l; ++_i) { | ||
params[_i].name = keys[_i]; | ||
} | ||
} | ||
@@ -250,0 +249,0 @@ } |
{ | ||
"name": "trek-router", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "A fast HTTP router", | ||
@@ -24,3 +24,3 @@ "repository": "trekjs/router", | ||
"devDependencies": { | ||
"babel": "^5.1.9", | ||
"babel": "^5.1.10", | ||
"benchmark": "^1.0.0", | ||
@@ -27,0 +27,0 @@ "isparta": "^3.x", |
12895