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