@@ -475,16 +475,18 @@ export type MatchFunction<P extends ParamData> = (path: string) => Match<P>;
475
475
* Create path match function from `path-to-regexp` spec.
476
476
*/
477
477
export function match < P extends ParamData > (
478
- path : Path ,
478
+ path : Path | Path [ ] ,
479
479
options : MatchOptions = { } ,
480
480
) : MatchFunction < P > {
481
- const { decode = decodeURIComponent , loose = true } = options ;
482
- const data = path instanceof TokenData ? path : parse ( path , options ) ;
483
- const stringify = toStringify ( loose , data . delimiter ) ;
484
- const keys : Key [ ] = [ ] ;
485
- const re = tokensToRegexp ( data , keys , options ) ;
481
+ const {
482
+ decode = decodeURIComponent ,
483
+ loose = true ,
484
+ delimiter = DEFAULT_DELIMITER ,
485
+ } = options ;
486
+ const re = pathToRegexp ( path , options ) ;
487
+ const stringify = toStringify ( loose , delimiter ) ;
486
488
487
- const decoders = keys . map ( ( key ) => {
489
+ const decoders = re . keys . map ( ( key ) => {
488
490
if ( decode && ( key . modifier === "+" || key . modifier === "*" ) ) {
489
491
const { prefix = "" , suffix = "" , separator = suffix + prefix } = key ;
490
492
const re = new RegExp ( stringify ( separator ) , "g" ) ;
@@ -504,7 +506,7 @@ export function match<P extends ParamData>(
504
506
for ( let i = 1 ; i < m . length ; i ++ ) {
505
507
if ( m [ i ] === undefined ) continue ;
506
508
507
- const key = keys [ i - 1 ] ;
509
+ const key = re . keys [ i - 1 ] ;
508
510
const decoder = decoders [ i - 1 ] ;
509
511
params [ key . name ] = decoder ( m [ i ] ) ;
510
512
}
@@ -517,7 +519,7 @@ export function match<P extends ParamData>(
517
519
* Escape a regular expression string.
518
520
*/
519
521
function escape ( str : string ) {
520
- return str . replace ( / ( [ . + * ? ^ $ { } ( ) [ \] | / \\ ] ) / g, "\\$1 " ) ;
522
+ return str . replace ( / [ . + * ? ^ $ { } ( ) [ \] | / \\ ] / g, "\\$& " ) ;
521
523
}
522
524
523
525
/**
@@ -565,27 +567,27 @@ export type Token = string | Key;
565
567
/**
566
568
* Expose a function for taking tokens and returning a RegExp.
567
569
*/
568
- function tokensToRegexp (
569
- data : TokenData ,
570
+ function pathToSource (
571
+ path : Path ,
570
572
keys : Key [ ] ,
573
+ flags : string ,
571
574
options : PathToRegexpOptions ,
572
- ) : RegExp {
575
+ ) : string {
576
+ const data = path instanceof TokenData ? path : parse ( path , options ) ;
573
577
const {
574
578
trailing = true ,
575
579
loose = true ,
576
580
start = true ,
577
581
end = true ,
578
582
strict = false ,
579
583
} = options ;
580
- const flags = toFlags ( options ) ;
581
584
const stringify = toStringify ( loose , data . delimiter ) ;
582
585
const sources = toRegExpSource ( data , stringify , keys , flags , strict ) ;
583
586
let pattern = start ? "^" : "" ;
584
587
pattern += sources . join ( "" ) ;
585
- if ( trailing ) pattern += `(?:${ stringify ( data . delimiter ) } )?` ;
588
+ if ( trailing ) pattern += `(?:${ stringify ( data . delimiter ) } $ )?` ;
586
589
pattern += end ? "$" : `(?=${ escape ( data . delimiter ) } |$)` ;
587
-
588
- return new RegExp ( pattern , flags ) ;
590
+ return pattern ;
589
591
}
590
592
591
593
/**
@@ -602,7 +604,7 @@ function toRegExpSource(
602
604
let backtrack = "" ;
603
605
let safe = true ;
604
606
605
- return data . tokens . map ( ( token , index ) => {
607
+ return data . tokens . map ( ( token ) => {
606
608
if ( typeof token === "string" ) {
607
609
backtrack = token ;
608
610
return stringify ( token ) ;
@@ -685,9 +687,18 @@ export type Path = string | TokenData;
685
687
* placeholder key descriptions. For example, using `/user/:id`, `keys` will
686
688
* contain `[{ name: 'id', delimiter: '/', optional: false, repeat: false }]`.
687
689
*/
688
- export function pathToRegexp ( path : Path , options : PathToRegexpOptions = { } ) {
689
- const data = path instanceof TokenData ? path : parse ( path , options ) ;
690
+ export function pathToRegexp (
691
+ path : Path | Path [ ] ,
692
+ options : PathToRegexpOptions = { } ,
693
+ ) {
690
694
const keys : Key [ ] = [ ] ;
691
- const regexp = tokensToRegexp ( data , keys , options ) ;
692
- return Object . assign ( regexp , { keys } ) ;
695
+ const flags = toFlags ( options ) ;
696
+
697
+ if ( Array . isArray ( path ) ) {
698
+ const regexps = path . map ( ( p ) => pathToSource ( p , keys , flags , options ) ) ;
699
+ return Object . assign ( new RegExp ( regexps . join ( "|" ) ) , { keys } ) ;
700
+ }
701
+
702
+ const regexp = pathToSource ( path , keys , flags , options ) ;
703
+ return Object . assign ( new RegExp ( regexp ) , { keys } ) ;
693
704
}
0 commit comments