@@ -125,8 +125,9 @@ function errorMessage(text: string, originalPath: string | undefined) {
125
125
/**
126
126
* Tokenize input string.
127
127
*/
128
- function * lexer ( str : string ) : Generator < LexToken , LexToken > {
128
+ function lexer ( str : string ) : Iter {
129
129
const chars = [ ...str ] ;
130
+ const tokens : Array < LexToken > = [ ] ;
130
131
let i = 0 ;
131
132
132
133
function name ( ) {
@@ -175,43 +176,44 @@ function* lexer(str: string): Generator<LexToken, LexToken> {
175
176
const type = SIMPLE_TOKENS [ value ] ;
176
177
177
178
if ( type ) {
178
- yield { type, index : i ++ , value } ;
179
+ tokens . push ( { type, index : i ++ , value } ) ;
179
180
} else if ( value === "\\" ) {
180
- yield { type : "ESCAPED" , index : i ++ , value : chars [ i ++ ] } ;
181
+ tokens . push ( { type : "ESCAPED" , index : i ++ , value : chars [ i ++ ] } ) ;
181
182
} else if ( value === ":" ) {
182
183
const value = name ( ) ;
183
- yield { type : "PARAM" , index : i , value } ;
184
+ tokens . push ( { type : "PARAM" , index : i , value } ) ;
184
185
} else if ( value === "*" ) {
185
186
const value = name ( ) ;
186
- yield { type : "WILDCARD" , index : i , value } ;
187
+ tokens . push ( { type : "WILDCARD" , index : i , value } ) ;
187
188
} else {
188
- yield { type : "CHAR" , index : i , value : chars [ i ++ ] } ;
189
+ tokens . push ( { type : "CHAR" , index : i , value : chars [ i ++ ] } ) ;
189
190
}
190
191
}
191
192
192
- return { type : "END" , index : i , value : "" } ;
193
+ tokens . push ( { type : "END" , index : i , value : "" } ) ;
194
+ return new Iter ( tokens , str ) ;
193
195
}
194
196
195
197
class Iter {
196
- private _peek ?: LexToken ;
197
- private _tokens : Generator < LexToken , LexToken > ;
198
+ private _tokens : Array < LexToken > ;
199
+ private _index = 0 ;
198
200
199
- constructor ( private originalPath : string ) {
200
- this . _tokens = lexer ( originalPath ) ;
201
+ constructor (
202
+ tokens : Array < LexToken > ,
203
+ private originalPath : string ,
204
+ ) {
205
+ this . _index = 0 ;
206
+ this . _tokens = tokens ;
201
207
}
202
208
203
209
peek ( ) : LexToken {
204
- if ( ! this . _peek ) {
205
- const next = this . _tokens . next ( ) ;
206
- this . _peek = next . value ;
207
- }
208
- return this . _peek ;
210
+ return this . _tokens [ this . _index ] ;
209
211
}
210
212
211
213
tryConsume ( type : TokenType ) : string | undefined {
212
214
const token = this . peek ( ) ;
213
215
if ( token . type !== type ) return ;
214
- this . _peek = undefined ; // Reset after consumed.
216
+ this . _index ++ ;
215
217
return token . value ;
216
218
}
217
219
@@ -299,7 +301,7 @@ export class TokenData {
299
301
*/
300
302
export function parse ( str : string , options : ParseOptions = { } ) : TokenData {
301
303
const { encodePath = NOOP_VALUE } = options ;
302
- const it = new Iter ( str ) ;
304
+ const it = lexer ( str ) ;
303
305
304
306
function consume ( endType : TokenType ) : Token [ ] {
305
307
const tokens : Token [ ] = [ ] ;
@@ -520,7 +522,14 @@ export function pathToRegexp(
520
522
} = options ;
521
523
const keys : Keys = [ ] ;
522
524
const flags = sensitive ? "" : "i" ;
523
- const sources = Array . from ( toRegExps ( path , delimiter , keys , options ) ) ;
525
+ const sources : string [ ] = [ ] ;
526
+
527
+ for ( const input of pathsToArray ( path , [ ] ) ) {
528
+ const data = input instanceof TokenData ? input : parse ( input , options ) ;
529
+ for ( const tokens of flatten ( data . tokens , 0 , [ ] ) ) {
530
+ sources . push ( toRegExp ( tokens , delimiter , keys , data . originalPath ) ) ;
531
+ }
532
+ }
524
533
525
534
let pattern = `^(?:${ sources . join ( "|" ) } )` ;
526
535
if ( trailing ) pattern += `(?:${ escape ( delimiter ) } $)?` ;
@@ -531,23 +540,15 @@ export function pathToRegexp(
531
540
}
532
541
533
542
/**
534
- * Path or array of paths to normalize .
543
+ * Convert a path or array of paths into a flat array .
535
544
*/
536
- function * toRegExps (
537
- path : Path | Path [ ] ,
538
- delimiter : string ,
539
- keys : Keys ,
540
- options : ParseOptions ,
541
- ) : Generator < string > {
542
- if ( Array . isArray ( path ) ) {
543
- for ( const p of path ) yield * toRegExps ( p , delimiter , keys , options ) ;
544
- return ;
545
- }
546
-
547
- const data = path instanceof TokenData ? path : parse ( path , options ) ;
548
- for ( const tokens of flatten ( data . tokens , 0 , [ ] ) ) {
549
- yield toRegExp ( tokens , delimiter , keys , data . originalPath ) ;
545
+ function pathsToArray ( paths : Path | Path [ ] , init : Path [ ] ) : Path [ ] {
546
+ if ( Array . isArray ( paths ) ) {
547
+ for ( const p of paths ) pathsToArray ( p , init ) ;
548
+ } else {
549
+ init . push ( paths ) ;
550
550
}
551
+ return init ;
551
552
}
552
553
553
554
/**
0 commit comments