@ -1,14 +1,17 @@
'use strict'
var utils = require ( './utils' )
var noop = utils . noop
var assign = utils . assign
var isFunction = utils . isFunction
var isRegExp = utils . isRegExp
var isString = utils . isString
var noop = utils . noop
function RouteNode ( options ) {
options = options || { }
this . _children = Object . create ( null )
this . _regExs = Object . create ( null )
this . _funcs = Object . create ( null )
}
assign ( RouteNode . prototype , {
@ -29,8 +32,10 @@ assign(RouteNode.prototype, {
if ( isString ( part ) ) {
node = this . _children [ part ]
} else {
} else if ( isRegExp ( part ) ) {
this . _regExs [ part ] = part
} else if ( isFunction ( part ) ) {
this . _funcs [ part ] = part
}
if ( node == null ) {
@ -56,42 +61,54 @@ assign(RouteNode.prototype, {
if ( results && results . node ) {
return results . node . get ( parts , args . concat ( results . args ) , done )
} else {
return done ( new Error ( 'not found' ) )
results = this . _getFunctionChild ( part )
if ( results && results . node ) {
return results . node . get ( parts , args . concat ( results . args ) , done )
} else {
return done ( new Error ( 'not found' ) )
}
}
}
} ,
_getRegexChild : function _getRegexChild ( part ) {
var childKey = null
var args = [ ]
var self = this
Object . keys ( this . _regExs ) . forEach ( function checkRegex ( key ) {
if ( childKey ) {
return
return Object . keys ( this . _regExs ) . reduce ( function checkRegex ( memo , key ) {
if ( memo ) {
return memo
}
var regex = self . _regExs [ key ]
var results = regex . exec ( part )
var lastIndex = null
if ( results ) {
childKey = key
if ( ! results ) {
return null
}
var lastIndex = null
var args = [ ]
while ( results && results . index !== lastIndex ) {
args = args . concat ( results [ 0 ] )
lastIndex = results . index
results = regex . exec ( part )
}
} )
return { args : args , key : key , node : self . _children [ key ] }
} , null )
} ,
if ( childKey ) {
return {
args : args ,
key : childKey ,
node : this . _children [ childKey ]
_getFunctionChild : function _getFunctionChild ( part ) {
var self = this
return Object . keys ( self . _funcs ) . reduce ( function checkFunc ( memo , key ) {
if ( memo ) {
return memo
}
} else {
return null
}
var func = self . _funcs [ key ]
var result = func ( part )
if ( result ) {
return { args : [ result ] , key : key , node : self . _children [ key ] }
} else {
return null
}
} , null )
}
} )