trek-router
Advanced tools
Comparing version 1.0.3 to 1.0.4
39
index.js
/*! | ||
* router | ||
* Copyright(c) 2015 Fangdun Cai | ||
* Copyright(c) 2015-2016 Fangdun Cai | ||
* MIT Licensed | ||
@@ -29,3 +29,3 @@ */ | ||
constructor(prefix, children, handler, pnames) { | ||
constructor (prefix, children, handler, pnames) { | ||
this.label = prefix.charCodeAt(0) | ||
@@ -44,3 +44,3 @@ this.prefix = prefix | ||
*/ | ||
findChild(c) { | ||
findChild (c) { | ||
const l = this.children.length | ||
@@ -67,5 +67,5 @@ let i = 0 | ||
constructor() { | ||
constructor () { | ||
this.trees = Object.create(null) | ||
METHODS.forEach((m) => { | ||
METHODS.forEach(m => { | ||
const M = m.toUpperCase() | ||
@@ -77,3 +77,3 @@ // Start from '/' | ||
Object.defineProperty(this, m, { | ||
value: function verb(path, handler) { | ||
value: function verb (path, handler) { | ||
return this.add(M, path, handler) | ||
@@ -93,3 +93,3 @@ } | ||
*/ | ||
add(method, path, handler) { | ||
add (method, path, handler) { | ||
let i = 0 | ||
@@ -137,3 +137,3 @@ let l = path.length | ||
*/ | ||
insert(method, path, handler, pnames) { | ||
insert (method, path, handler, pnames) { | ||
let cn = this.trees[method] // Current node as root | ||
@@ -148,11 +148,12 @@ let search = path | ||
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) { | ||
// 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) { | ||
if (l < pl) { | ||
// Split node | ||
@@ -209,3 +210,3 @@ n = new Node(cn.prefix.substring(l), cn.children, cn.handler, cn.pnames) | ||
*/ | ||
find(method, path, cn, n, result) { | ||
find (method, path, cn, n, result) { | ||
cn = cn || this.trees[method] // Current node as root | ||
@@ -297,3 +298,3 @@ n = n | 0 // Param count | ||
// Length of longest common prefix | ||
function lcp(a, b) { | ||
function lcp (a, b) { | ||
const max = min(a.length, b.length) | ||
@@ -300,0 +301,0 @@ let i = 0 |
{ | ||
"name": "trek-router", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "A fast HTTP router", | ||
@@ -42,3 +42,3 @@ "repository": "trekjs/router", | ||
"dependencies": { | ||
"methods": "^1.x" | ||
"methods": "1.x" | ||
}, | ||
@@ -45,0 +45,0 @@ "files": [ |
@@ -73,2 +73,8 @@ # trek-router | ||
let params = {} | ||
if (result[0]) { | ||
result[1].forEach(param => params[param.name] = param.value) | ||
// => { id: 233 } | ||
} | ||
// Not Found | ||
@@ -75,0 +81,0 @@ let result = router.find('GET', '/photos/233') |
13274
263
124
Updatedmethods@1.x