1
1
/* @flow */
2
2
3
- import Regexp from 'path-to-regexp'
4
3
import { assert , warn } from './util/warn'
5
4
import { createRoute } from './util/route'
6
5
import { createRouteMap } from './create-route-map'
7
6
import { resolvePath } from './util/path'
8
7
import { normalizeLocation } from './util/location'
9
-
10
- const regexpCache : {
11
- [ key : string ] : {
12
- keys : Array < ?{ name : string } > ,
13
- regexp : RegExp
14
- }
15
- } = Object . create ( null )
16
-
17
- const regexpParamsCache : {
18
- [ key : string ] : Array < string >
19
- } = Object . create ( null )
20
-
21
- const regexpCompileCache : {
22
- [ key : string ] : Function
23
- } = Object . create ( null )
8
+ import { getRouteRegex , fillParams } from './util/params'
24
9
25
10
export function createMatcher ( routes : Array < RouteConfig > ) : Matcher {
26
11
const { pathMap, nameMap } = createRouteMap ( routes )
@@ -35,10 +20,9 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
35
20
36
21
if ( name ) {
37
22
const record = nameMap [ name ]
38
- const paramNames = regexpParamsCache [ record . path ] ||
39
- ( regexpParamsCache [ record . path ] = getRouteRegex ( record . path ) . keys
40
- . filter ( key => ! key . optional )
41
- . map ( key => key . name ) )
23
+ const paramNames = getRouteRegex ( record . path ) . keys
24
+ . filter ( key => ! key . optional )
25
+ . map ( key => key . name )
42
26
43
27
if ( typeof location . params !== 'object' ) {
44
28
location . params = { }
@@ -158,22 +142,6 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
158
142
return match
159
143
}
160
144
161
- function getRouteRegex ( path : string ) : Object {
162
- const hit = regexpCache [ path ]
163
- let keys , regexp
164
-
165
- if ( hit ) {
166
- keys = hit . keys
167
- regexp = hit . regexp
168
- } else {
169
- keys = [ ]
170
- regexp = Regexp ( path , keys )
171
- regexpCache [ path ] = { keys, regexp }
172
- }
173
-
174
- return { keys, regexp }
175
- }
176
-
177
145
function matchRoute (
178
146
path : string ,
179
147
params : Object ,
@@ -197,22 +165,6 @@ function matchRoute (
197
165
return true
198
166
}
199
167
200
- function fillParams (
201
- path : string ,
202
- params : ?Object ,
203
- routeMsg : string
204
- ) : string {
205
- try {
206
- const filler =
207
- regexpCompileCache [ path ] ||
208
- ( regexpCompileCache [ path ] = Regexp . compile ( path ) )
209
- return filler ( params || { } , { pretty : true } )
210
- } catch ( e ) {
211
- assert ( false , `missing param for ${ routeMsg } : ${ e . message } ` )
212
- return ''
213
- }
214
- }
215
-
216
168
function resolveRecordPath ( path : string , record : RouteRecord ) : string {
217
169
return resolvePath ( path , record . parent ? record . parent . path : '/' , true )
218
170
}
0 commit comments