1- import pathToRegexp from 'path-to-regexp' ;
1+ import { pathToRegexp , Keys } from 'path-to-regexp' ;
22import { type Match } from './Route' ;
33
44const patternCache = { } ;
55const cacheLimit = 10000 ;
66let cacheCount = 0 ;
77
8- interface pathToRegexKey {
9- name : string | number ;
10- prefix : string ;
11- delimiter : string ;
12- optional : boolean ;
13- repeat : boolean ;
14- pattern : string ;
15- partial : boolean ;
16- asterisk : boolean ;
17- }
18-
19- const compilePath = ( pattern , options ) : { re : any ; keys : pathToRegexKey [ ] } => {
8+ const compilePath = ( pattern , options ) : { re : any ; keys : Keys } => {
209 const cacheKey = `${ options . end } ${ options . strict } ${ options . sensitive } ` ;
2110 const cache = patternCache [ cacheKey ] || ( patternCache [ cacheKey ] = { } ) ;
2211
2312 if ( cache [ pattern ] ) {
2413 return cache [ pattern ] ;
2514 }
2615
27- const keys = [ ] ;
28- const re = pathToRegexp ( pattern , keys , options ) ;
16+ options . trailing = options . end && ! options . strict ;
17+ pattern = pattern . replace ( / \? / , '{.:optqspunct}' ) ;
18+ if ( ! options . exact && ! options . strict ) {
19+ pattern = pattern . replace ( / \/ $ / , '{.:optendslash}' ) ;
20+ }
21+
22+ const { regexp : re , keys } = pathToRegexp ( pattern , options )
2923 const compiledPattern = { re, keys } ;
3024
3125 if ( cacheCount < cacheLimit ) {
@@ -73,10 +67,11 @@ export function matchPath(pathname, options: any): Match<any> | null {
7367 loader,
7468 loaderData,
7569 params : keys . reduce ( ( memo , key , index ) => {
76- memo [ key . name ] = values [ index ] ;
70+ if ( values [ index ] !== undefined )
71+ memo [ key . name ] = values [ index ] ;
7772 return memo ;
7873 } , { } ) ,
7974 path, // the path pattern used to match
8075 url : path === '/' && url === '' ? '/' : url , // the matched portion of the URL
8176 } ;
82- }
77+ }
0 commit comments