@@ -6,8 +6,14 @@ Object.defineProperty(exports, '__esModule', {
66
77var _createClass = ( function ( ) { function defineProperties ( target , props ) { for ( var i = 0 ; i < props . length ; i ++ ) { var descriptor = props [ i ] ; descriptor . enumerable = descriptor . enumerable || false ; descriptor . configurable = true ; if ( 'value' in descriptor ) descriptor . writable = true ; Object . defineProperty ( target , descriptor . key , descriptor ) ; } } return function ( Constructor , protoProps , staticProps ) { if ( protoProps ) defineProperties ( Constructor . prototype , protoProps ) ; if ( staticProps ) defineProperties ( Constructor , staticProps ) ; return Constructor ; } ; } ) ( ) ;
88
9+ function _interopRequireDefault ( obj ) { return obj && obj . __esModule ? obj : { 'default' : obj } ; }
10+
911function _classCallCheck ( instance , Constructor ) { if ( ! ( instance instanceof Constructor ) ) { throw new TypeError ( 'Cannot call a class as a function' ) ; } }
1012
13+ var _pathParser = require ( 'path-parser' ) ;
14+
15+ var _pathParser2 = _interopRequireDefault ( _pathParser ) ;
16+
1117var RouteNode = ( function ( ) {
1218 function RouteNode ( ) {
1319 var name = arguments [ 0 ] === undefined ? '' : arguments [ 0 ] ;
@@ -18,7 +24,11 @@ var RouteNode = (function () {
1824
1925 this . name = name ;
2026 this . path = path ;
27+ if ( path ) {
28+ this . parser = new _pathParser2 [ 'default' ] ( path ) ;
29+ }
2130 this . children = [ ] ;
31+
2232 this . add ( childRoutes ) ;
2333 }
2434
@@ -43,19 +53,24 @@ var RouteNode = (function () {
4353 }
4454 route = new RouteNode ( route . name , route . path , route . children ) ;
4555 }
56+ // Check duplicated routes
57+ if ( this . children . map ( function ( child ) {
58+ return child . name ;
59+ } ) . indexOf ( route . name ) !== - 1 ) {
60+ throw new Error ( 'Alias "' + route . name + '" is already defined in route node' ) ;
61+ }
62+ // Check duplicated paths
63+ if ( this . children . map ( function ( child ) {
64+ return child . path ;
65+ } ) . indexOf ( route . path ) !== - 1 ) {
66+ throw new Error ( 'Path "' + route . path + '" is already defined in route node' ) ;
67+ }
4668
47- // if (this.nameMap[route.name]) {
48- // throw new Error(`Alias $route.name is already defined in route node`)
49- // }
50-
51- // if (this.pathMap[route.name]) {
52- // throw new Error(`Path $route.path is already defined in route node`)
53- // }
5469 this . children . push ( route ) ;
5570 }
5671 } , {
57- key : 'findRoute ' ,
58- value : function findRoute ( path ) { }
72+ key : 'findRouteByPath ' ,
73+ value : function findRouteByPath ( path ) { }
5974 } , {
6075 key : 'findRouteByName' ,
6176 value : function findRouteByName ( routeName ) {
@@ -90,6 +105,17 @@ var RouteNode = (function () {
90105 return segment . path ;
91106 } ) . join ( '' ) ;
92107 }
108+ } , {
109+ key : 'buildPath' ,
110+ value : function buildPath ( routeName ) {
111+ var params = arguments [ 1 ] === undefined ? { } : arguments [ 1 ] ;
112+
113+ var segments = this . findRouteByName ( routeName ) ;
114+
115+ return segments . map ( function ( segment ) {
116+ return segment . parser . build ( params ) ;
117+ } ) . join ( '' ) ;
118+ }
93119 } ] ) ;
94120
95121 return RouteNode ;
0 commit comments