@@ -351,17 +351,18 @@ function tokenToFunction(
351351 throw new TypeError ( `Expected "${ token . name } " to be a non-empty array` ) ;
352352 }
353353
354- return value
355- . map ( ( value , index ) => {
356- if ( typeof value !== "string" ) {
357- throw new TypeError (
358- `Expected "${ token . name } /${ index } " to be a string` ,
359- ) ;
360- }
354+ let result = "" ;
361355
362- return encodeValue ( value ) ;
363- } )
364- . join ( delimiter ) ;
356+ for ( let i = 0 ; i < value . length ; i ++ ) {
357+ if ( typeof value [ i ] !== "string" ) {
358+ throw new TypeError ( `Expected "${ token . name } /${ i } " to be a string` ) ;
359+ }
360+
361+ if ( i > 0 ) result += delimiter ;
362+ result += encodeValue ( value [ i ] ) ;
363+ }
364+
365+ return result ;
365366 } ;
366367 }
367368
@@ -453,29 +454,30 @@ export function pathToRegexp(
453454 trailing = true ,
454455 } = options ;
455456 const keys : Keys = [ ] ;
456- const sources : string [ ] = [ ] ;
457- const paths : Array < Path | Path [ ] > = [ path ] ;
457+ let source = "" ;
458458 let combinations = 0 ;
459459
460- while ( paths . length ) {
461- const path = paths . shift ( ) ! ;
462-
460+ function process ( path : Path | Path [ ] ) {
463461 if ( Array . isArray ( path ) ) {
464- paths . push ( ... path ) ;
465- continue ;
462+ for ( const p of path ) process ( p ) ;
463+ return ;
466464 }
467465
468466 const data = typeof path === "object" ? path : parse ( path , options ) ;
469467 flatten ( data . tokens , 0 , [ ] , ( tokens ) => {
470- if ( combinations ++ >= 256 ) {
468+ if ( combinations >= 256 ) {
471469 throw new PathError ( "Too many path combinations" , data . originalPath ) ;
472470 }
473471
474- sources . push ( toRegExpSource ( tokens , delimiter , keys , data . originalPath ) ) ;
472+ if ( combinations > 0 ) source += "|" ;
473+ source += toRegExpSource ( tokens , delimiter , keys , data . originalPath ) ;
474+ combinations ++ ;
475475 } ) ;
476476 }
477477
478- let pattern = `^(?:${ sources . join ( "|" ) } )` ;
478+ process ( path ) ;
479+
480+ let pattern = `^(?:${ source } )` ;
479481 if ( trailing ) pattern += "(?:" + escape ( delimiter ) + "$)?" ;
480482 pattern += end ? "$" : "(?=" + escape ( delimiter ) + "|$)" ;
481483
@@ -495,9 +497,11 @@ function flatten(
495497 const token = tokens [ index ++ ] ;
496498
497499 if ( token . type === "group" ) {
498- flatten ( token . tokens , 0 , result . slice ( ) , ( seq ) =>
500+ const len = result . length ;
501+ flatten ( token . tokens , 0 , result , ( seq ) =>
499502 flatten ( tokens , index , seq , callback ) ,
500503 ) ;
504+ result . length = len ;
501505 continue ;
502506 }
503507
0 commit comments