@@ -29,7 +29,7 @@ function Parth(){
2929// --
3030//
3131
32- util . paramRE = / ( ^ | \W ) \: ( [ ^ ( ) ? # \. \/ ] + ) ( \( . + ? \) ) ? / g;
32+ util . paramRE = / ( ^ | \W ) \: ( [ ^ ( ) ? # \. \/ ] + ) ( \( + [ ^ ] * \) + ) ? / g;
3333
3434Parth . prototype . set = function ( p , o ) {
3535 o = o || { } ; if ( ! util . boil ( p , o ) ) { return this ; }
@@ -38,11 +38,11 @@ Parth.prototype.set = function(p, o){
3838 var store = this . store ;
3939
4040 // number of default and custom regex
41- o . custom = o . default = o . depth ;
41+ o . custom = o . default = 0 ;
4242 o . regex = '^' + o . path . replace ( / \S + / g, function ( stem ) {
4343 o . sep = ( / \/ / ) . test ( stem ) ? '/#?' : '.' ;
4444 return stem . replace ( util . paramRE , function ( $0 , $1 , $2 , $3 ) {
45- if ( $3 ) { o . custom -- ; } else { o . default -- ; }
45+ if ( $3 ) { o . custom ++ ; } else { o . default ++ ; }
4646 return $1 + ( $3 || '([^' + o . sep + '^]+)' ) ;
4747 } ) ;
4848 } ) . replace ( / [ \/ \. ] / g, '\\$&' ) // scape path tokens
@@ -64,7 +64,7 @@ Parth.prototype.set = function(p, o){
6464 // reorder them
6565 store . regex [ o . depth ] . push ( o . regex ) ;
6666 store . regex [ o . depth ] = store . regex [ o . depth ] . sort ( function ( a , b ) {
67- return ( ( a . def - 2 * a . cust ) + ( b . def - 2 * b . cust ) ) || - Infinity ;
67+ return ( ( a . def - a . cust ) - ( b . def - b . cust ) ) ;
6868 } ) ;
6969
7070 // sum up all learned: void groups and make it one
@@ -107,18 +107,24 @@ Parth.prototype.get = function(p, o){
107107 } else { o . depth -- ; }
108108 }
109109
110- o . index = o . found . indexOf ( o . found . join ( '' ) ) ;
110+ var found = o . found . join ( '' ) ;
111+ o . index = o . found . indexOf ( found ) ;
111112 o . regex = this . store . regex [ o . depth ] [ o . index ] ;
112113
113114 o . index = 0 ;
114115 o . params = { _ : o . path . match ( o . regex ) . slice ( 1 ) } ;
115- o . notFound = o . path . replace (
116- o . regex . path . replace ( util . paramRE , function ( $0 , $1 , $2 ) {
117- var p = o . params . _ [ o . index ] ;
118- o . params [ $2 ] = o . params . _ [ o . index ++ ] = Number ( p ) || p ;
119- return $1 + p ;
120- } ) , '' ) [ 0 ] || ' ' ;
121-
122- o . notFound = ! ( / ^ [ ] / ) . test ( o . notFound ) ;
123- return o . regex ;
116+ o . regex . path . replace ( util . paramRE , function ( $0 , $1 , $2 ) {
117+ var p = o . params . _ [ o . index ] ;
118+ o . params [ $2 ] = o . params . _ [ o . index ++ ] = Number ( p ) || p ;
119+ return $1 + p ;
120+ } ) ;
121+
122+ o . notFound = Boolean ( o . path . replace ( found , '' ) . trim ( ) ) ;
123+ return util . merge ( new RegExp ( o . regex . source ) , {
124+ notFound : o . notFound ,
125+ url : o . url ,
126+ path : o . regex . path ,
127+ argv : o . regex . argv . slice ( ) ,
128+ params : o . params
129+ } ) ;
124130} ;
0 commit comments