trek-router
Advanced tools
Comparing version 0.5.1 to 0.5.2
@@ -13,8 +13,4 @@ /*! | ||
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } } | ||
var _methods = require('methods'); | ||
@@ -40,7 +36,5 @@ | ||
*/ | ||
class Node { | ||
var Node = (function () { | ||
function Node(prefix, children, handler, pnames) { | ||
_classCallCheck(this, Node); | ||
constructor(prefix, children, handler, pnames) { | ||
this.label = prefix.charCodeAt(0); | ||
@@ -54,9 +48,2 @@ this.prefix = prefix; | ||
/** | ||
* Router | ||
* | ||
* @class Router | ||
* @constructor | ||
*/ | ||
/** | ||
* Find child node by charCode | ||
@@ -67,28 +54,28 @@ * | ||
*/ | ||
findChild(c) { | ||
var i = 0; | ||
var l = this.children.length; | ||
var e = undefined; | ||
_createClass(Node, [{ | ||
key: 'findChild', | ||
value: function findChild(c) { | ||
var i = 0; | ||
var l = this.children.length; | ||
var e = undefined; | ||
for (; i < l; ++i) { | ||
e = this.children[i]; | ||
// Compare charCode | ||
if (e.label === c) return e; | ||
} | ||
return undefined; | ||
for (; i < l; ++i) { | ||
e = this.children[i]; | ||
// Compare charCode | ||
if (e.label === c) return e; | ||
} | ||
}]); | ||
return undefined; | ||
} | ||
return Node; | ||
})(); | ||
} | ||
var Router = (function () { | ||
function Router() { | ||
/** | ||
* Router | ||
* | ||
* @class Router | ||
* @constructor | ||
*/ | ||
class Router { | ||
constructor() { | ||
var _this = this; | ||
_classCallCheck(this, Router); | ||
this.trees = Object.create(null); | ||
@@ -110,4 +97,2 @@ _methods2['default'].forEach(function (m) { | ||
// Length of longest common prefix | ||
/** | ||
@@ -121,218 +106,210 @@ * Add new route | ||
*/ | ||
add(method, path, handler) { | ||
var i = 0; | ||
var l = path.length; | ||
var pnames = []; // Param names | ||
var ch = undefined, | ||
j = undefined; | ||
_createClass(Router, [{ | ||
key: 'add', | ||
value: function add(method, path, handler) { | ||
var i = 0; | ||
var l = path.length; | ||
var pnames = []; // Param names | ||
var ch = undefined, | ||
j = undefined; | ||
for (; i < l; ++i) { | ||
ch = path.charCodeAt(i); | ||
if (ch === COLON) { | ||
j = i + 1; | ||
for (; i < l; ++i) { | ||
ch = path.charCodeAt(i); | ||
if (ch === COLON) { | ||
j = i + 1; | ||
this.insert(method, path.substring(0, i)); | ||
for (; i < l && path.charCodeAt(i) !== SLASH; ++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)); | ||
} else if (ch === STAR) { | ||
this.insert(method, path.substring(0, i)); | ||
this.insert(method, path.substring(0, l), handler, pnames); | ||
if (i === l) { | ||
this.insert(method, path.substring(0, i), handler, pnames); | ||
return; | ||
} | ||
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, handler, pnames); | ||
} | ||
this.insert(method, path, handler, pnames); | ||
} | ||
/** | ||
* Insert new route | ||
* | ||
* @method insert | ||
* @private | ||
* @param {String} method | ||
* @param {String} path | ||
* @param {Function|GeneratorFunction} [handler] | ||
* @param {Array} [pnames] | ||
*/ | ||
}, { | ||
key: 'insert', | ||
value: function insert(method, path, handler, pnames) { | ||
var cn = this.trees[method]; // Current node as root | ||
var search = path; | ||
var sl = undefined, | ||
pl = undefined, | ||
l = undefined, | ||
n = undefined, | ||
c = undefined; | ||
/** | ||
* Insert new route | ||
* | ||
* @method insert | ||
* @private | ||
* @param {String} method | ||
* @param {String} path | ||
* @param {Function|GeneratorFunction} [handler] | ||
* @param {Array} [pnames] | ||
*/ | ||
insert(method, path, handler, pnames) { | ||
var cn = this.trees[method]; // Current node as root | ||
var search = path; | ||
var sl = undefined, | ||
pl = undefined, | ||
l = undefined, | ||
n = undefined, | ||
c = undefined; | ||
while (true) { | ||
sl = search.length; | ||
pl = cn.prefix.length; | ||
l = lcp(search, cn.prefix); | ||
while (true) { | ||
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; | ||
if (handler) { | ||
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 | ||
if (l === 0) { | ||
// At root node | ||
cn.label = search.charCodeAt(0); | ||
cn.prefix = search; | ||
if (handler) { | ||
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 | ||
// Reset parent node | ||
cn.label = cn.prefix.charCodeAt(0); | ||
cn.prefix = cn.prefix.substring(0, l); | ||
cn.handler = undefined; | ||
cn.pnames = undefined; | ||
// Reset parent node | ||
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; | ||
} else { | ||
// Create child node | ||
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)); | ||
if (c !== undefined) { | ||
// Go deeper | ||
cn = c; | ||
continue; | ||
} | ||
if (l === sl) { | ||
// At parent node | ||
cn.handler = handler; | ||
cn.pnames = pnames; | ||
} else { | ||
// Create child node | ||
n = new Node(search, [], handler, pnames); | ||
n = new Node(search.substring(l), [], handler, pnames); | ||
cn.children.push(n); | ||
} else { | ||
// Node already exists | ||
if (handler) { | ||
cn.handler = handler; | ||
cn.pnames = pnames; | ||
} | ||
} | ||
return; | ||
} else if (l < sl) { | ||
search = search.substring(l); | ||
c = cn.findChild(search.charCodeAt(0)); | ||
if (c !== undefined) { | ||
// Go deeper | ||
cn = c; | ||
continue; | ||
} | ||
// Create child node | ||
n = new Node(search, [], handler, pnames); | ||
cn.children.push(n); | ||
} else { | ||
// Node already exists | ||
if (handler) { | ||
cn.handler = handler; | ||
cn.pnames = pnames; | ||
} | ||
} | ||
return; | ||
} | ||
} | ||
/** | ||
* Find route by method and path | ||
* | ||
* @method find | ||
* @param {String} method | ||
* @param {String} path | ||
* @return {Array} result | ||
* @property {Undefined|Function|GeneratorFunction} result[0] | ||
* @property {Array} result[1] | ||
*/ | ||
}, { | ||
key: 'find', | ||
value: function find(method, path, cn, n, result) { | ||
cn = cn || this.trees[method]; // Current node as root | ||
n = n || 0; // Param count | ||
result = result || [undefined, []]; | ||
var search = path; | ||
var params = result[1]; // Params | ||
var pl = undefined, | ||
l = undefined, | ||
leq = undefined, | ||
c = undefined; | ||
var preSearch = undefined; // Pre search | ||
/** | ||
* Find route by method and path | ||
* | ||
* @method find | ||
* @param {String} method | ||
* @param {String} path | ||
* @return {Array} result | ||
* @property {Undefined|Function|GeneratorFunction} result[0] | ||
* @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, []]; | ||
var search = path; | ||
var params = result[1]; // Params | ||
var pl = undefined, | ||
l = undefined, | ||
leq = undefined, | ||
c = undefined; | ||
var preSearch = undefined; // Pre search | ||
// Search order static > param > match-any | ||
if (search.length === 0 || search === cn.prefix) { | ||
// Found | ||
result[0] = cn.handler; | ||
if (cn.handler !== undefined) { | ||
var pnames = cn.pnames; | ||
if (pnames !== undefined) { | ||
var _i = 0; | ||
var _l = pnames.length; | ||
for (; _i < _l; ++_i) { | ||
params[_i].name = pnames[_i]; | ||
} | ||
// Search order static > param > match-any | ||
if (search.length === 0 || search === cn.prefix) { | ||
// Found | ||
result[0] = cn.handler; | ||
if (cn.handler !== undefined) { | ||
var pnames = cn.pnames; | ||
if (pnames !== undefined) { | ||
var _i = 0; | ||
var _l = pnames.length; | ||
for (; _i < _l; ++_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); | ||
} | ||
preSearch = search; | ||
if (leq) { | ||
search = search.substring(l); | ||
} | ||
preSearch = search; | ||
// Static node | ||
c = cn.findChild(search.charCodeAt(0)); | ||
if (c !== undefined) { | ||
this.find(method, search, c, n, result); | ||
if (result[0] !== undefined) return result; | ||
search = preSearch; | ||
} | ||
// Static node | ||
c = cn.findChild(search.charCodeAt(0)); | ||
if (c !== undefined) { | ||
this.find(method, search, c, n, result); | ||
if (result[0] !== undefined) return result; | ||
search = preSearch; | ||
} | ||
// Not found node | ||
if (!leq) { | ||
return result; | ||
} | ||
// Not found node | ||
if (!leq) { | ||
return result; | ||
} | ||
// Param node | ||
c = cn.findChild(COLON); | ||
if (c !== undefined) { | ||
l = search.length; | ||
for (var i = 0; i < l && search.charCodeAt(i) !== SLASH; ++i) {} | ||
// Param node | ||
c = cn.findChild(COLON); | ||
if (c !== undefined) { | ||
l = search.length; | ||
for (var i = 0; i < l && search.charCodeAt(i) !== SLASH; ++i) {} | ||
params[n] = { | ||
value: search.substring(0, 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--; | ||
params.shift(); | ||
search = preSearch; | ||
} | ||
n--; | ||
params.shift(); | ||
search = preSearch; | ||
} | ||
// Match-any node | ||
c = cn.findChild(STAR); | ||
if (c !== undefined) { | ||
params[n] = { | ||
name: '_*', | ||
value: search | ||
}; | ||
search = ''; // End search | ||
this.find(method, search, c, n, result); | ||
} | ||
return result; | ||
// Match-any node | ||
c = cn.findChild(STAR); | ||
if (c !== undefined) { | ||
params[n] = { | ||
name: '_*', | ||
value: search | ||
}; | ||
search = ''; // End search | ||
this.find(method, search, c, n, result); | ||
} | ||
}]); | ||
return Router; | ||
})(); | ||
return result; | ||
} | ||
} | ||
// Length of longest common prefix | ||
function lcp(a, b) { | ||
@@ -339,0 +316,0 @@ var i = 0; |
{ | ||
"name": "trek-router", | ||
"version": "0.5.1", | ||
"version": "0.5.2", | ||
"description": "A fast HTTP router", | ||
@@ -53,2 +53,3 @@ "repository": "trekjs/router", | ||
"regenerator", | ||
"es6.classes", | ||
"es6.constants", | ||
@@ -55,0 +56,0 @@ "es6.properties.computed", |
13944
277