You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
9 years ago
|
'use strict'
|
||
|
var utils = require('./utils')
|
||
|
|
||
|
var noop = utils.noop
|
||
|
var assign = utils.assign
|
||
|
|
||
|
function RouteNode (options) {
|
||
|
options = options || {}
|
||
|
this._debug = !!options.debug
|
||
|
this._exact = Object.create(null)
|
||
|
}
|
||
|
|
||
|
assign(RouteNode.prototype, {
|
||
|
handler: null,
|
||
|
|
||
|
add: function add(parts, handler, context) {
|
||
|
parts = parts || []
|
||
|
handler = handler || noop
|
||
|
context = context || null
|
||
|
|
||
|
if (parts.length === 0) {
|
||
|
this.handler = handler
|
||
|
this.context = context
|
||
|
return this
|
||
|
}
|
||
|
var part = parts.shift()
|
||
|
var node = this._exact[part]
|
||
|
if (node == null) {
|
||
|
node = new RouteNode({debug: this._debug})
|
||
|
this._exact[part] = node
|
||
|
}
|
||
|
return node.add(parts, handler, context)
|
||
|
},
|
||
|
|
||
|
get: function get(parts, args, done) {
|
||
|
parts = parts || []
|
||
|
args = args || []
|
||
|
|
||
|
if (parts.length === 0) {
|
||
|
return done(null, this.handler, this.context, args)
|
||
|
}
|
||
|
var part = parts.shift()
|
||
|
var childNode = this._exact[part]
|
||
|
if (childNode) {
|
||
|
return childNode.get(parts, args, done)
|
||
|
} else {
|
||
|
return done(new Error('not found'))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
})
|
||
|
|
||
|
module.exports = RouteNode
|