11/* @flow */
22
3- import Regexp from 'path-to-regexp'
43import { assert , warn } from './util/warn'
54import { createRoute } from './util/route'
65import { createRouteMap } from './create-route-map'
76import { resolvePath } from './util/path'
87import { 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'
249
2510export function createMatcher ( routes : Array < RouteConfig > ) : Matcher {
2611 const { pathMap, nameMap } = createRouteMap ( routes )
@@ -35,10 +20,9 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
3520
3621 if ( name ) {
3722 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 )
4226
4327 if ( typeof location . params !== 'object' ) {
4428 location . params = { }
@@ -158,22 +142,6 @@ export function createMatcher (routes: Array<RouteConfig>): Matcher {
158142 return match
159143}
160144
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-
177145function matchRoute (
178146 path : string ,
179147 params : Object ,
@@ -197,22 +165,6 @@ function matchRoute (
197165 return true
198166}
199167
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-
216168function resolveRecordPath ( path : string , record : RouteRecord ) : string {
217169 return resolvePath ( path , record . parent ? record . parent . path : '/' , true )
218170}
0 commit comments