Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Apr 15, 2015
1 parent d127c7c commit 404385b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
53 changes: 26 additions & 27 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,18 @@ const COLON = 58; // ':'
* @param {String} path
* @param {Array} [children]
* @param {Function|GeneratorFunction} handler
* @param {Array} [keys]
*/

var Node = (function () {
function Node(prefix, children, handler) {
function Node(prefix, children, handler, keys) {
_classCallCheck(this, Node);

this.label = prefix.charCodeAt(0);
this.prefix = prefix;
this.children = children || [];
this.handler = handler;
this.keys = keys;
}

/**
Expand Down Expand Up @@ -90,12 +92,10 @@ var Router = (function () {
*/

Router.prototype.add = function add(method, path, handler) {
// Store param keys
var keys = [];
if (handler) handler.keys = keys;

var i = 0;
var l = path.length;
// Store param keys
var keys = [];
var ch = undefined,
j = undefined;

Expand All @@ -117,16 +117,16 @@ var Router = (function () {
l = path.length;

if (i === l) {
this.insert(method, path.substring(0, i), handler);
this.insert(method, path.substring(0, i), handler, keys);
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);
this.insert(method, path.substring(0, l), handler, keys);
}
}
this.insert(method, path, handler);
this.insert(method, path, handler, keys);
};

/**
Expand All @@ -139,7 +139,7 @@ var Router = (function () {
* @param {Function|GeneratorFunction} handler
*/

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
var search = path;
var sl = undefined,
Expand All @@ -157,25 +157,26 @@ var Router = (function () {
// At root node
cn.label = search.charCodeAt(0);
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

// Reset parent node
cn.label = cn.prefix.charCodeAt(0);
cn.prefix = cn.prefix.substring(0, l);
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);
}
} else if (l < sl) {
Expand All @@ -187,13 +188,12 @@ var Router = (function () {
continue;
}
// 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;
}
return;
}
Expand All @@ -214,9 +214,6 @@ var Router = (function () {
n = n || 0; // Param count
cn = cn || this.trees[method]; // Current node as root
result = result || [undefined, []];
// let n = 0; // Param count
// let cn = this.trees[method]; // Current node as root
// let result = [undefined, []];
var search = path;
var params = result[1];
var pl = undefined,
Expand All @@ -230,11 +227,13 @@ var Router = (function () {
result[0] = cn.handler;
result[1] = params;
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];
}
}
}
return result;
Expand Down
51 changes: 25 additions & 26 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ const COLON = 58; // ':'
* @param {String} path
* @param {Array} [children]
* @param {Function|GeneratorFunction} handler
* @param {Array} [keys]
*/
class Node {

constructor(prefix, children, handler) {
constructor(prefix, children, handler, keys) {
this.label = prefix.charCodeAt(0);
this.prefix = prefix;
this.children = children || [];
this.handler = handler;
this.keys = keys;
}

/**
Expand Down Expand Up @@ -83,12 +85,10 @@ class Router {
* @param {Function|GeneratorFunction} handler
*/
add(method, path, handler) {
// Store param keys
let keys = [];
if (handler) handler.keys = keys;

let i = 0;
let l = path.length
// Store param keys
let keys = [];
let ch, j;

for (; i < l; ++i) {
Expand All @@ -109,16 +109,16 @@ class Router {
l = path.length;

if (i === l) {
this.insert(method, path.substring(0, i), handler);
this.insert(method, path.substring(0, i), handler, keys);
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);
this.insert(method, path.substring(0, l), handler, keys);
}
}
this.insert(method, path, handler);
this.insert(method, path, handler, keys);
}

/**
Expand All @@ -130,7 +130,7 @@ class Router {
* @param {String} path
* @param {Function|GeneratorFunction} handler
*/
insert(method, path, handler) {
insert(method, path, handler, keys) {
let cn = this.trees[method]; // Current node as root
let search = path;
let sl, pl, l, n, e;
Expand All @@ -144,25 +144,26 @@ class Router {
// At root node
cn.label = search.charCodeAt(0);
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

// Reset parent node
cn.label = cn.prefix.charCodeAt(0);
cn.prefix = cn.prefix.substring(0, l);
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);
}
} else if (l < sl) {
Expand All @@ -174,13 +175,12 @@ class Router {
continue;
}
// 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;
}
return;
}
Expand All @@ -200,9 +200,6 @@ class Router {
n = n || 0; // Param count
cn = cn || this.trees[method]; // Current node as root
result = result || [undefined, []];
// let n = 0; // Param count
// let cn = this.trees[method]; // Current node as root
// let result = [undefined, []];
let search = path;
let params = result[1];
let pl, l, leq, e;
Expand All @@ -213,10 +210,12 @@ class Router {
result[0] = cn.handler;
result[1] = params;
if (cn.handler !== undefined) {
let keys = cn.handler.keys;
let i = 0, l = keys.length;
for (; i < l; ++i) {
params[i].name = keys[i];
let keys = cn.keys;
if (keys !== undefined) {
let i = 0, l = keys.length;
for (; i < l; ++i) {
params[i].name = keys[i];
}
}
}
return result;
Expand Down

0 comments on commit 404385b

Please sign in to comment.