Skip to content

Commit 9dae1f9

Browse files
committed
Add custom separator
1 parent f971b7d commit 9dae1f9

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

src/index.spec.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,6 +1614,9 @@ const MATCH_TESTS: MatchTestSet[] = [
16141614
},
16151615
{
16161616
path: "{:test}*",
1617+
testOptions: {
1618+
skip: true,
1619+
},
16171620
tests: [
16181621
{
16191622
input: "test",
@@ -1638,6 +1641,9 @@ const MATCH_TESTS: MatchTestSet[] = [
16381641
},
16391642
{
16401643
path: "{:test}+",
1644+
testOptions: {
1645+
skip: true,
1646+
},
16411647
tests: [
16421648
{
16431649
input: "test",

src/index.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
const DEFAULT_DELIMITER = "/";
2-
const DEFAULT_PREFIXES = "./";
32
const NOOP_VALUE = (value: string) => value;
43
const ID_CHAR = /^\p{XID_Continue}$/u;
54

@@ -289,7 +288,6 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
289288
pattern: `[^${escape(delimiter)}]*`,
290289
modifier: "*",
291290
separator: delimiter,
292-
optional: true,
293291
});
294292
continue;
295293
}
@@ -300,15 +298,11 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
300298
const name = it.tryConsume("NAME");
301299
const pattern = it.tryConsume("PATTERN") || "";
302300
const suffix = it.text();
301+
const separator = it.tryConsume(";") ? it.text() : prefix + suffix;
303302

304303
it.consume("}");
305304

306305
const modifier = it.modifier();
307-
const optional = modifier === "?" || modifier === "*";
308-
const separator =
309-
modifier === "*" || modifier === "+"
310-
? prefix + suffix || delimiter
311-
: undefined;
312306

313307
tokens.push({
314308
name: name || (pattern ? String(key++) : ""),
@@ -317,7 +311,6 @@ export function parse(str: string, options: ParseOptions = {}): TokenData {
317311
pattern,
318312
modifier,
319313
separator,
320-
optional,
321314
});
322315
continue;
323316
}
@@ -355,8 +348,10 @@ function tokenToFunction(
355348
}
356349

357350
const encodeValue = encode || NOOP_VALUE;
351+
const repeated = token.modifier === "+" || token.modifier === "*";
352+
const optional = token.modifier === "?" || token.modifier === "*";
358353

359-
if (encode && token.separator) {
354+
if (encode && repeated) {
360355
const stringify = (value: string, index: number) => {
361356
if (typeof value !== "string") {
362357
throw new TypeError(`Expected "${token.name}/${index}" to be a string`);
@@ -376,7 +371,7 @@ function tokenToFunction(
376371
);
377372
};
378373

379-
if (token.optional) {
374+
if (optional) {
380375
return (data): string => {
381376
const value = data[token.name];
382377
if (value == null) return "";
@@ -397,7 +392,7 @@ function tokenToFunction(
397392
return token.prefix + encodeValue(value) + token.suffix;
398393
};
399394

400-
if (token.optional) {
395+
if (optional) {
401396
return (data): string => {
402397
const value = data[token.name];
403398
if (value == null) return "";
@@ -488,8 +483,8 @@ export function match<P extends ParamData>(
488483
const re = tokensToRegexp(data, keys, options);
489484

490485
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");
493488
return (value: string) => value.split(re).map(decode);
494489
}
495490

@@ -556,7 +551,6 @@ export interface Key {
556551
pattern: string;
557552
modifier: string;
558553
separator?: string;
559-
optional?: boolean;
560554
}
561555

562556
/**
@@ -604,13 +598,12 @@ function toKeyRegexp(stringify: Encode, delimiter: string) {
604598

605599
if (key.name) {
606600
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 || "");
610604
return `(?:${prefix}((?:${pattern})(?:${split}(?:${pattern}))*)${suffix})${mod}`;
611-
} else {
612-
return `(?:${prefix}(${pattern})${suffix})${mod}`;
613605
}
606+
return `(?:${prefix}(${pattern})${suffix})${key.modifier}`;
614607
}
615608

616609
return `(?:${prefix}${suffix})${key.modifier}`;

0 commit comments

Comments
 (0)