@@ -24,9 +24,7 @@ var RouteNode = (function () {
2424
2525 this . name = name ;
2626 this . path = path ;
27- if ( path ) {
28- this . parser = new _pathParser2 [ 'default' ] ( path ) ;
29- }
27+ this . parser = path ? new _pathParser2 [ 'default' ] ( path ) : { } ;
3028 this . children = [ ] ;
3129
3230 this . add ( childRoutes ) ;
@@ -69,11 +67,8 @@ var RouteNode = (function () {
6967 this . children . push ( route ) ;
7068 }
7169 } , {
72- key : 'findRouteByPath' ,
73- value : function findRouteByPath ( path ) { }
74- } , {
75- key : 'findRouteByName' ,
76- value : function findRouteByName ( routeName ) {
70+ key : '_findRouteByName' ,
71+ value : function _findRouteByName ( routeName ) {
7772 var findSegmentByName = function findSegmentByName ( name , routes ) {
7873 var filteredRoutes = routes . filter ( function ( r ) {
7974 return r . name === name ;
@@ -96,10 +91,57 @@ var RouteNode = (function () {
9691
9792 return matched ? segments : [ ] ;
9893 }
94+ } , {
95+ key : 'matchRoute' ,
96+ value : function matchRoute ( path ) {
97+ var segments = [ ] ;
98+
99+ var matchChildren = function matchChildren ( node , pathSegment , matched ) {
100+ var _loop = function ( i ) {
101+ var child = node . children [ i ] ;
102+ var match = child . parser . partialMatch ( pathSegment ) ;
103+ if ( match ) {
104+ // Append name and extend params
105+ matched . name += ( matched . name ? '.' : '' ) + child . name ;
106+ Object . keys ( match ) . forEach ( function ( p ) {
107+ return matched . params [ p ] = match [ p ] ;
108+ } ) ;
109+ // Remove consumed segment from path
110+ var remainingPath = pathSegment . replace ( child . parser . build ( match ) , '' ) ;
111+ // If fully matched
112+ if ( ! remainingPath . length && ! child . children . length ) {
113+ return {
114+ v : matched
115+ } ;
116+ }
117+ // If no children to match against but segment left
118+ if ( ! child . children . length ) {
119+ return {
120+ v : false
121+ } ;
122+ }
123+ // Else: remaining path and children
124+ return {
125+ v : matchChildren ( child , remainingPath , matched )
126+ } ;
127+ }
128+ } ;
129+
130+ // for (child of node.children) {
131+ for ( var i in node . children ) {
132+ var _ret = _loop ( i ) ;
133+
134+ if ( typeof _ret === 'object' ) return _ret . v ;
135+ }
136+ return false ;
137+ } ;
138+
139+ return matchChildren ( this , path , { name : '' , params : { } } ) ;
140+ }
99141 } , {
100142 key : 'getPath' ,
101143 value : function getPath ( routeName ) {
102- var segments = this . findRouteByName ( routeName ) ;
144+ var segments = this . _findRouteByName ( routeName ) ;
103145
104146 return segments . map ( function ( segment ) {
105147 return segment . path ;
@@ -110,7 +152,7 @@ var RouteNode = (function () {
110152 value : function buildPath ( routeName ) {
111153 var params = arguments [ 1 ] === undefined ? { } : arguments [ 1 ] ;
112154
113- var segments = this . findRouteByName ( routeName ) ;
155+ var segments = this . _findRouteByName ( routeName ) ;
114156
115157 return segments . map ( function ( segment ) {
116158 return segment . parser . build ( params ) ;
0 commit comments