1+ const DEFAULT_PREFIXES = "./" ;
2+ const DEFAULT_DELIMITER = "/#?" ;
3+ const DEFAULT_ENCODE = ( x : string ) => x ;
4+ const DEFAULT_DECODE = ( x : string ) => x ;
5+
16/**
27 * Tokenizer results.
38 */
@@ -138,10 +143,11 @@ export interface ParseOptions {
138143 * Parse a string for the raw tokens.
139144 */
140145export 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 ) } ]+?` ;
144149 const result : Token [ ] = [ ] ;
150+ const tokens = lexer ( str ) ;
145151 let key = 0 ;
146152 let i = 0 ;
147153 let path = "" ;
@@ -265,7 +271,7 @@ export function tokensToFunction<P extends object = object>(
265271 options : TokensToFunctionOptions = { } ,
266272) : PathFunction < P > {
267273 const reFlags = flags ( options ) ;
268- const { encode = ( x : string ) => x , validate = true } = options ;
274+ const { encode = DEFAULT_ENCODE , validate = true } = options ;
269275
270276 // Compile all the tokens into regexps.
271277 const matches = tokens . map ( ( token ) => {
@@ -388,7 +394,7 @@ export function regexpToFunction<P extends object = object>(
388394 keys : Key [ ] ,
389395 options : RegexpToFunctionOptions = { } ,
390396) : MatchFunction < P > {
391- const { decode = ( x : string ) => x } = options ;
397+ const { decode = DEFAULT_DECODE } = options ;
392398
393399 return function ( pathname : string ) {
394400 const m = re . exec ( pathname ) ;
@@ -452,19 +458,17 @@ function regexpToRegexp(path: RegExp, keys?: Key[]): RegExp {
452458 if ( ! keys ) return path ;
453459
454460 const groupsRegex = / \( (?: \? < ( .* ?) > ) ? (? ! \? ) / g;
455-
456461 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 ) ) ) {
459464 keys . push ( {
460- // Use parenthesized substring match if available, index otherwise
465+ // Use parenthesized substring match if available, index otherwise.
461466 name : execResult [ 1 ] || index ++ ,
462467 prefix : "" ,
463468 suffix : "" ,
464469 modifier : "" ,
465470 pattern : "" ,
466471 } ) ;
467- execResult = groupsRegex . exec ( path . source ) ;
468472 }
469473
470474 return path ;
@@ -536,8 +540,8 @@ export function tokensToRegexp(
536540 strict = false ,
537541 start = true ,
538542 end = true ,
539- encode = ( x : string ) => x ,
540- delimiter = "/#?" ,
543+ encode = DEFAULT_ENCODE ,
544+ delimiter = DEFAULT_DELIMITER ,
541545 endsWith = "" ,
542546 } = options ;
543547 const endsWithRe = `[${ escapeString ( endsWith ) } ]|$` ;
@@ -563,11 +567,7 @@ export function tokensToRegexp(
563567 route += `(?:${ prefix } (${ token . pattern } )${ suffix } )${ token . modifier } ` ;
564568 }
565569 } 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 } )` ;
571571 }
572572 } else {
573573 route += `(?:${ prefix } ${ suffix } )${ token . modifier } ` ;
@@ -578,13 +578,13 @@ export function tokensToRegexp(
578578 if ( end ) {
579579 if ( ! strict ) route += `${ delimiterRe } ?` ;
580580
581- route += ! options . endsWith ? "$" : `(?=${ endsWithRe } )` ;
581+ route += options . endsWith ? `(?=${ endsWithRe } )` : "$" ;
582582 } else {
583583 const endToken = tokens [ tokens . length - 1 ] ;
584584 const isEndDelimited =
585585 typeof endToken === "string"
586- ? delimiterRe . indexOf ( endToken [ endToken . length - 1 ] ) > - 1
587- : endToken === undefined ;
586+ ? delimiter . indexOf ( endToken [ endToken . length - 1 ] ) > - 1
587+ : ! endToken ;
588588
589589 if ( ! strict ) {
590590 route += `(?:${ delimiterRe } (?=${ endsWithRe } ))?` ;
0 commit comments