@@ -463,7 +463,8 @@ export function pathToRegexp(
463463 sensitive = false ,
464464 trailing = true ,
465465 } = options ;
466- const root = new SourceNode ( "^" ) ;
466+ const keys : Keys = [ ] ;
467+ const sources : string [ ] = [ ] ;
467468 const paths : Array < Path | Path [ ] > = [ path ] ;
468469 let combinations = 0 ;
469470
@@ -481,49 +482,17 @@ export function pathToRegexp(
481482 throw new PathError ( "Too many path combinations" , data . originalPath ) ;
482483 }
483484
484- let node = root ;
485-
486- for ( const part of toRegExpSource ( tokens , delimiter , data . originalPath ) ) {
487- node = node . add ( part . source , part . key ) ;
488- }
489-
490- node . add ( "" ) ; // Mark the end of the source.
485+ sources . push ( toRegExpSource ( tokens , delimiter , keys , data . originalPath ) ) ;
491486 } ) ;
492487 }
493488
494- const keys : Keys = [ ] ;
495- let pattern = toRegExp ( root , keys ) ;
489+ let pattern = `^(?:${ sources . join ( "|" ) } )` ;
496490 if ( trailing ) pattern += "(?:" + escape ( delimiter ) + "$)?" ;
497491 pattern += end ? "$" : "(?=" + escape ( delimiter ) + "|$)" ;
498492
499493 return { regexp : new RegExp ( pattern , sensitive ? "" : "i" ) , keys } ;
500494}
501495
502- function toRegExp ( node : SourceNode , keys : Keys ) : string {
503- if ( node . key ) keys . push ( node . key ) ;
504-
505- const children = Object . keys ( node . children ) ;
506- const text = children
507- . map ( ( id ) => toRegExp ( node . children [ id ] , keys ) )
508- . join ( "|" ) ;
509-
510- return node . source + ( children . length < 2 ? text : `(?:${ text } )` ) ;
511- }
512-
513- class SourceNode {
514- children : Record < string , SourceNode > = Object . create ( null ) ;
515-
516- constructor (
517- public source : string ,
518- public key ?: Key ,
519- ) { }
520-
521- add ( source : string , key ?: Key ) {
522- const id = source + ":" + ( key ? key . name : "" ) ;
523- return ( this . children [ id ] ||= new SourceNode ( source , key ) ) ;
524- }
525- }
526-
527496/**
528497 * Generate a flat list of sequence tokens from the given tokens.
529498 */
@@ -549,23 +518,16 @@ function flatten(
549518 callback ( result ) ;
550519}
551520
552- /**
553- * Simplest token for the trie deduplication.
554- */
555- interface RegExpPart {
556- source : string ;
557- key ?: Key ;
558- }
559-
560521/**
561522 * Transform a flat sequence of tokens into a regular expression.
562523 */
563524function toRegExpSource (
564525 tokens : Exclude < Token , Group > [ ] ,
565526 delimiter : string ,
527+ keys : Keys ,
566528 originalPath : string | undefined ,
567- ) : RegExpPart [ ] {
568- let result : RegExpPart [ ] = [ ] ;
529+ ) : string {
530+ let result = "" ;
569531 let backtrack = "" ;
570532 let wildcardBacktrack = "" ;
571533 let prevCaptureType : 0 | 1 | 2 = 0 ;
@@ -597,7 +559,7 @@ function toRegExpSource(
597559 const token = tokens [ index ++ ] ;
598560
599561 if ( token . type === "text" ) {
600- result . push ( { source : escape ( token . value ) } ) ;
562+ result += escape ( token . value ) ;
601563 backtrack += token . value ;
602564 if ( prevCaptureType === 2 ) wildcardBacktrack += token . value ;
603565 if ( token . value . includes ( delimiter ) ) hasSegmentCapture = 0 ;
@@ -613,34 +575,29 @@ function toRegExpSource(
613575 }
614576
615577 if ( token . type === "param" ) {
616- result . push ( {
617- source :
618- hasSegmentCapture & 2 // Seen wildcard in segment.
619- ? `(${ negate ( delimiter , backtrack ) } +)`
620- : hasInSegment ( index , "wildcard" ) // See wildcard later in segment.
621- ? `(${ negate ( delimiter , peekText ( index ) ) } +)`
622- : hasSegmentCapture & 1 // Seen parameter in segment.
623- ? `(${ negate ( delimiter , backtrack ) } +|${ escape ( backtrack ) } )`
624- : `(${ negate ( delimiter , "" ) } +?)` ,
625- key : token ,
626- } ) ;
578+ result +=
579+ hasSegmentCapture & 2 // Seen wildcard in segment.
580+ ? `(${ negate ( delimiter , backtrack ) } +)`
581+ : hasInSegment ( index , "wildcard" ) // See wildcard later in segment.
582+ ? `(${ negate ( delimiter , peekText ( index ) ) } +)`
583+ : hasSegmentCapture & 1 // Seen parameter in segment.
584+ ? `(${ negate ( delimiter , backtrack ) } +|${ escape ( backtrack ) } )`
585+ : `(${ negate ( delimiter , "" ) } +)` ;
627586
628587 hasSegmentCapture |= prevCaptureType = 1 ;
629588 } else {
630- result . push ( {
631- source :
632- hasSegmentCapture & 2 // Seen wildcard in segment.
633- ? `(${ negate ( backtrack , "" ) } +)`
634- : wildcardBacktrack // No capture in segment, seen wildcard in path.
635- ? `(${ negate ( wildcardBacktrack , "" ) } +|${ negate ( delimiter , "" ) } +)`
636- : `([^]+?)` ,
637- key : token ,
638- } ) ;
589+ result +=
590+ hasSegmentCapture & 2 // Seen wildcard in segment.
591+ ? `(${ negate ( backtrack , "" ) } +)`
592+ : wildcardBacktrack // No capture in segment, seen wildcard in path.
593+ ? `(${ negate ( wildcardBacktrack , "" ) } +|${ negate ( delimiter , "" ) } +)`
594+ : `([^]+)` ;
639595
640596 wildcardBacktrack = "" ;
641597 hasSegmentCapture |= prevCaptureType = 2 ;
642598 }
643599
600+ keys . push ( token ) ;
644601 backtrack = "" ;
645602 continue ;
646603 }
0 commit comments