Skip to content

Commit

Permalink
Bump v1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
fundon committed Apr 9, 2016
1 parent 9cfefb2 commit 1c70760
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ let result = router.find('GET', '/users/233')
// => [handler, params]
// => [()=>{}, [{name: id, value: 233}]]

let params
let params = {}
if (result[0]) {
result[1].forEach(param => params[param.name] = param.value)
// => { id: 233 }
Expand Down
39 changes: 20 additions & 19 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*!
* router
* Copyright(c) 2015 Fangdun Cai
* Copyright(c) 2015-2016 Fangdun Cai
* MIT Licensed
*/

Expand All @@ -26,7 +26,7 @@ const COLON = 58 // ':'
*/
class Node {

constructor(prefix, children, handler, pnames) {
constructor (prefix, children, handler, pnames) {
this.label = prefix.charCodeAt(0)
this.prefix = prefix
this.children = children || []
Expand All @@ -40,7 +40,7 @@ class Node {
* @param {Number} char code
* @return {Node|undefined} node
*/
findChild(c) {
findChild (c) {
const l = this.children.length
let i = 0
let e
Expand All @@ -62,16 +62,16 @@ class Node {
*/
class Router {

constructor() {
constructor () {
this.trees = Object.create(null)
METHODS.forEach((m) => {
METHODS.forEach(m => {
const M = m.toUpperCase()
// Start from '/'
Object.defineProperty(this.trees, M, {
value: new Node('/')
})
Object.defineProperty(this, m, {
value: function verb(path, handler) {
value: function verb (path, handler) {
return this.add(M, path, handler)
}
})
Expand All @@ -86,7 +86,7 @@ class Router {
* @param {String} path
* @param {Function} handler
*/
add(method, path, handler) {
add (method, path, handler) {
let i = 0
let l = path.length
let pnames = [] // Param names
Expand Down Expand Up @@ -129,7 +129,7 @@ class Router {
* @param {Function} [handler]
* @param {Array} [pnames]
*/
insert(method, path, handler, pnames) {
insert (method, path, handler, pnames) {
let cn = this.trees[method] // Current node as root
let search = path
let sl, pl, l, n, c
Expand All @@ -139,15 +139,16 @@ class Router {
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) {
// 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
n = new Node(cn.prefix.substring(l), cn.children, cn.handler, cn.pnames)
cn.children = [n] // Add to parent
Expand Down Expand Up @@ -199,7 +200,7 @@ class Router {
* @property {Undefined|Function} result[0]
* @property {Array} result[1]
*/
find(method, path, cn, n, result) {
find (method, path, cn, n, result) {
cn = cn || this.trees[method] // Current node as root
n = n | 0 // Param count
result = result || [undefined, []]
Expand Down Expand Up @@ -286,7 +287,7 @@ class Router {
}

// Length of longest common prefix
function lcp(a, b) {
function lcp (a, b) {
const max = min(a.length, b.length)
let i = 0
for (; i < max && (a.charCodeAt(i) === b.charCodeAt(i)); ++i) {}
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "trek-router",
"version": "1.0.3",
"version": "1.0.4",
"description": "A fast HTTP router",
"repository": "trekjs/router",
"main": "index.js",
Expand Down Expand Up @@ -39,7 +39,7 @@
"node": ">= 4"
},
"dependencies": {
"methods": "^1.x"
"methods": "1.x"
},
"files": [
"HISTORY.md",
Expand Down

0 comments on commit 1c70760

Please sign in to comment.