@@ -44,13 +44,13 @@ function getIndexRoute(route, location, callback) {
44
44
}
45
45
46
46
function assignParams ( params , paramNames , paramValues ) {
47
- return paramNames . reduceRight ( function ( params , paramName , index ) {
47
+ return paramNames . reduce ( function ( params , paramName , index ) {
48
48
const paramValue = paramValues && paramValues [ index ]
49
49
50
50
if ( Array . isArray ( params [ paramName ] ) ) {
51
- params [ paramName ] . unshift ( paramValue )
51
+ params [ paramName ] . push ( paramValue )
52
52
} else if ( paramName in params ) {
53
- params [ paramName ] = [ paramValue , params [ paramName ] ]
53
+ params [ paramName ] = [ params [ paramName ] , paramValue ]
54
54
} else {
55
55
params [ paramName ] = paramValue
56
56
}
@@ -63,34 +63,46 @@ function createParams(paramNames, paramValues) {
63
63
return assignParams ( { } , paramNames , paramValues )
64
64
}
65
65
66
- function matchRouteDeep ( basename , route , location , callback ) {
66
+ function matchRouteDeep (
67
+ route , location , remainingPathname , paramNames , paramValues , callback
68
+ ) {
67
69
let pattern = route . path || ''
68
70
69
- if ( pattern . charAt ( 0 ) !== '/' )
70
- pattern = basename . replace ( / \/ * $ / , '/' ) + pattern // Relative paths build on the parent's path.
71
+ if ( pattern . charAt ( 0 ) === '/' ) {
72
+ remainingPathname = location . pathname
73
+ paramNames = [ ]
74
+ paramValues = [ ]
75
+ }
71
76
72
- const { remainingPathname, paramNames, paramValues } = matchPattern ( pattern , location . pathname )
73
- const isExactMatch = remainingPathname === ''
77
+ if ( remainingPathname !== null ) {
78
+ const matched = matchPattern ( pattern , remainingPathname )
79
+ remainingPathname = matched . remainingPathname
80
+ paramNames = [ ...paramNames , ...matched . paramNames ]
81
+ paramValues = [ ...paramValues , ...matched . paramValues ]
74
82
75
- if ( isExactMatch && route . path ) {
76
- const match = {
77
- routes : [ route ] ,
78
- params : createParams ( paramNames , paramValues )
79
- }
83
+ if ( remainingPathname === '' && route . path ) {
84
+ const match = {
85
+ routes : [ route ] ,
86
+ params : createParams ( paramNames , paramValues )
87
+ }
80
88
81
- getIndexRoute ( route , location , function ( error , indexRoute ) {
82
- if ( error ) {
83
- callback ( error )
84
- } else {
85
- if ( Array . isArray ( indexRoute ) )
86
- match . routes . push ( ...indexRoute )
87
- else if ( indexRoute )
88
- match . routes . push ( indexRoute )
89
+ getIndexRoute ( route , location , function ( error , indexRoute ) {
90
+ if ( error ) {
91
+ callback ( error )
92
+ } else {
93
+ if ( Array . isArray ( indexRoute ) )
94
+ match . routes . push ( ...indexRoute )
95
+ else if ( indexRoute )
96
+ match . routes . push ( indexRoute )
89
97
90
- callback ( null , match )
91
- }
92
- } )
93
- } else if ( remainingPathname != null || route . childRoutes ) {
98
+ callback ( null , match )
99
+ }
100
+ } )
101
+ return
102
+ }
103
+ }
104
+
105
+ if ( remainingPathname != null || route . childRoutes ) {
94
106
// Either a) this route matched at least some of the path or b)
95
107
// we don't have to load this route's children asynchronously. In
96
108
// either case continue checking for matches in the subtree.
@@ -109,7 +121,7 @@ function matchRouteDeep(basename, route, location, callback) {
109
121
} else {
110
122
callback ( )
111
123
}
112
- } , pattern )
124
+ } , remainingPathname , paramNames , paramValues )
113
125
} else {
114
126
callback ( )
115
127
}
@@ -130,15 +142,21 @@ function matchRouteDeep(basename, route, location, callback) {
130
142
* Note: This operation may finish synchronously if no routes have an
131
143
* asynchronous getChildRoutes method.
132
144
*/
133
- function matchRoutes ( routes , location , callback , basename = '' ) {
145
+ function matchRoutes (
146
+ routes , location , callback ,
147
+ remainingPathname = location . pathname , paramNames = [ ] , paramValues = [ ]
148
+ ) {
134
149
loopAsync ( routes . length , function ( index , next , done ) {
135
- matchRouteDeep ( basename , routes [ index ] , location , function ( error , match ) {
136
- if ( error || match ) {
137
- done ( error , match )
138
- } else {
139
- next ( )
150
+ matchRouteDeep (
151
+ routes [ index ] , location , remainingPathname , paramNames , paramValues ,
152
+ function ( error , match ) {
153
+ if ( error || match ) {
154
+ done ( error , match )
155
+ } else {
156
+ next ( )
157
+ }
140
158
}
141
- } )
159
+ )
142
160
} , callback )
143
161
}
144
162
0 commit comments