1
+ const DEFAULT_PREFIXES = "./" ;
2
+ const DEFAULT_DELIMITER = "/#?" ;
3
+ const DEFAULT_ENCODE = ( x : string ) => x ;
4
+ const DEFAULT_DECODE = ( x : string ) => x ;
5
+
1
6
/**
2
7
* Tokenizer results.
3
8
*/
@@ -138,10 +143,11 @@ export interface ParseOptions {
138
143
* Parse a string for the raw tokens.
139
144
*/
140
145
export function parse ( str : string , options : ParseOptions = { } ) : Token [ ] {
141
- const tokens = lexer ( str ) ;
142
- const { prefixes = "./" } = options ;
143
- const defaultPattern = `[^${ escapeString ( options . delimiter || "/#?" ) } ]+?` ;
146
+ const { prefixes = DEFAULT_PREFIXES , delimiter = DEFAULT_DELIMITER } =
147
+ options ;
148
+ const defaultPattern = `[^${ escapeString ( delimiter ) } ]+?` ;
144
149
const result : Token [ ] = [ ] ;
150
+ const tokens = lexer ( str ) ;
145
151
let key = 0 ;
146
152
let i = 0 ;
147
153
let path = "" ;
@@ -265,7 +271,7 @@ export function tokensToFunction<P extends object = object>(
265
271
options : TokensToFunctionOptions = { } ,
266
272
) : PathFunction < P > {
267
273
const reFlags = flags ( options ) ;
268
- const { encode = ( x : string ) => x , validate = true } = options ;
274
+ const { encode = DEFAULT_ENCODE , validate = true } = options ;
269
275
270
276
// Compile all the tokens into regexps.
271
277
const matches = tokens . map ( ( token ) => {
@@ -388,7 +394,7 @@ export function regexpToFunction<P extends object = object>(
388
394
keys : Key [ ] ,
389
395
options : RegexpToFunctionOptions = { } ,
390
396
) : MatchFunction < P > {
391
- const { decode = ( x : string ) => x } = options ;
397
+ const { decode = DEFAULT_DECODE } = options ;
392
398
393
399
return function ( pathname : string ) {
394
400
const m = re . exec ( pathname ) ;
@@ -452,19 +458,17 @@ function regexpToRegexp(path: RegExp, keys?: Key[]): RegExp {
452
458
if ( ! keys ) return path ;
453
459
454
460
const groupsRegex = / \( (?: \? < ( .* ?) > ) ? (? ! \? ) / g;
455
-
456
461
let index = 0 ;
457
- let execResult = groupsRegex . exec ( path . source ) ;
458
- while ( execResult ) {
462
+ let execResult : RegExpExecArray | null = null ;
463
+ while ( ( execResult = groupsRegex . exec ( path . source ) ) ) {
459
464
keys . push ( {
460
- // Use parenthesized substring match if available, index otherwise
465
+ // Use parenthesized substring match if available, index otherwise.
461
466
name : execResult [ 1 ] || index ++ ,
462
467
prefix : "" ,
463
468
suffix : "" ,
464
469
modifier : "" ,
465
470
pattern : "" ,
466
471
} ) ;
467
- execResult = groupsRegex . exec ( path . source ) ;
468
472
}
469
473
470
474
return path ;
@@ -536,8 +540,8 @@ export function tokensToRegexp(
536
540
strict = false ,
537
541
start = true ,
538
542
end = true ,
539
- encode = ( x : string ) => x ,
540
- delimiter = "/#?" ,
543
+ encode = DEFAULT_ENCODE ,
544
+ delimiter = DEFAULT_DELIMITER ,
541
545
endsWith = "" ,
542
546
} = options ;
543
547
const endsWithRe = `[${ escapeString ( endsWith ) } ]|$` ;
@@ -563,11 +567,7 @@ export function tokensToRegexp(
563
567
route += `(?:${ prefix } (${ token . pattern } )${ suffix } )${ token . modifier } ` ;
564
568
}
565
569
} else {
566
- if ( token . modifier === "+" || token . modifier === "*" ) {
567
- route += `((?:${ token . pattern } )${ token . modifier } )` ;
568
- } else {
569
- route += `(${ token . pattern } )${ token . modifier } ` ;
570
- }
570
+ route += `((?:${ token . pattern } )${ token . modifier } )` ;
571
571
}
572
572
} else {
573
573
route += `(?:${ prefix } ${ suffix } )${ token . modifier } ` ;
@@ -578,13 +578,13 @@ export function tokensToRegexp(
578
578
if ( end ) {
579
579
if ( ! strict ) route += `${ delimiterRe } ?` ;
580
580
581
- route += ! options . endsWith ? "$" : `(?=${ endsWithRe } )` ;
581
+ route += options . endsWith ? `(?=${ endsWithRe } )` : "$" ;
582
582
} else {
583
583
const endToken = tokens [ tokens . length - 1 ] ;
584
584
const isEndDelimited =
585
585
typeof endToken === "string"
586
- ? delimiterRe . indexOf ( endToken [ endToken . length - 1 ] ) > - 1
587
- : endToken === undefined ;
586
+ ? delimiter . indexOf ( endToken [ endToken . length - 1 ] ) > - 1
587
+ : ! endToken ;
588
588
589
589
if ( ! strict ) {
590
590
route += `(?:${ delimiterRe } (?=${ endsWithRe } ))?` ;
0 commit comments