1
1
const DEFAULT_DELIMITER = "/" ;
2
- const DEFAULT_PREFIXES = "./" ;
3
2
const NOOP_VALUE = ( value : string ) => value ;
4
3
const ID_CHAR = / ^ \p{ XID_Continue} $ / u;
5
4
@@ -289,7 +288,6 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
289
288
pattern : `[^${ escape ( delimiter ) } ]*` ,
290
289
modifier : "*" ,
291
290
separator : delimiter ,
292
- optional : true ,
293
291
} ) ;
294
292
continue ;
295
293
}
@@ -300,15 +298,11 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
300
298
const name = it . tryConsume ( "NAME" ) ;
301
299
const pattern = it . tryConsume ( "PATTERN" ) || "" ;
302
300
const suffix = it . text ( ) ;
301
+ const separator = it . tryConsume ( ";" ) ? it . text ( ) : prefix + suffix ;
303
302
304
303
it . consume ( "}" ) ;
305
304
306
305
const modifier = it . modifier ( ) ;
307
- const optional = modifier === "?" || modifier === "*" ;
308
- const separator =
309
- modifier === "*" || modifier === "+"
310
- ? prefix + suffix || delimiter
311
- : undefined ;
312
306
313
307
tokens . push ( {
314
308
name : name || ( pattern ? String ( key ++ ) : "" ) ,
@@ -317,7 +311,6 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
317
311
pattern,
318
312
modifier,
319
313
separator,
320
- optional,
321
314
} ) ;
322
315
continue ;
323
316
}
@@ -355,8 +348,10 @@ function tokenToFunction(
355
348
}
356
349
357
350
const encodeValue = encode || NOOP_VALUE ;
351
+ const repeated = token . modifier === "+" || token . modifier === "*" ;
352
+ const optional = token . modifier === "?" || token . modifier === "*" ;
358
353
359
- if ( encode && token . separator ) {
354
+ if ( encode && repeated ) {
360
355
const stringify = ( value : string , index : number ) => {
361
356
if ( typeof value !== "string" ) {
362
357
throw new TypeError ( `Expected "${ token . name } /${ index } " to be a string` ) ;
@@ -376,7 +371,7 @@ function tokenToFunction(
376
371
) ;
377
372
} ;
378
373
379
- if ( token . optional ) {
374
+ if ( optional ) {
380
375
return ( data ) : string => {
381
376
const value = data [ token . name ] ;
382
377
if ( value == null ) return "" ;
@@ -397,7 +392,7 @@ function tokenToFunction(
397
392
return token . prefix + encodeValue ( value ) + token . suffix ;
398
393
} ;
399
394
400
- if ( token . optional ) {
395
+ if ( optional ) {
401
396
return ( data ) : string => {
402
397
const value = data [ token . name ] ;
403
398
if ( value == null ) return "" ;
@@ -488,8 +483,8 @@ export function match<P extends ParamData>(
488
483
const re = tokensToRegexp ( data , keys , options ) ;
489
484
490
485
const decoders = keys . map ( ( key ) => {
491
- if ( decode && key . separator ) {
492
- const re = new RegExp ( stringify ( key . separator ) , "g" ) ;
486
+ if ( decode && ( key . modifier === "+" || key . modifier === "*" ) ) {
487
+ const re = new RegExp ( stringify ( key . separator || "" ) , "g" ) ;
493
488
return ( value : string ) => value . split ( re ) . map ( decode ) ;
494
489
}
495
490
@@ -556,7 +551,6 @@ export interface Key {
556
551
pattern : string ;
557
552
modifier : string ;
558
553
separator ?: string ;
559
- optional ?: boolean ;
560
554
}
561
555
562
556
/**
@@ -604,13 +598,12 @@ function toKeyRegexp(stringify: Encode, delimiter: string) {
604
598
605
599
if ( key . name ) {
606
600
const pattern = key . pattern || segmentPattern ;
607
- const mod = key . optional ? "?" : "" ;
608
- if ( key . separator ) {
609
- const split = stringify ( key . separator ) ;
601
+ if ( key . modifier === "+" || key . modifier === "*" ) {
602
+ const mod = key . modifier === "*" ? "?" : "" ;
603
+ const split = stringify ( key . separator || "" ) ;
610
604
return `(?:${ prefix } ((?:${ pattern } )(?:${ split } (?:${ pattern } ))*)${ suffix } )${ mod } ` ;
611
- } else {
612
- return `(?:${ prefix } (${ pattern } )${ suffix } )${ mod } ` ;
613
605
}
606
+ return `(?:${ prefix } (${ pattern } )${ suffix } )${ key . modifier } ` ;
614
607
}
615
608
616
609
return `(?:${ prefix } ${ suffix } )${ key . modifier } ` ;
0 commit comments