Skip to content

Commit

Permalink
Standalone modifiers act like unnamed params
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed May 20, 2024
1 parent 8b4d1fb commit 246ec9f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 33 deletions.
38 changes: 19 additions & 19 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1705,69 +1705,69 @@ const TESTS: Test[] = [
"/*",
undefined,
[
"/",
{
name: 0,
prefix: "",
prefix: "/",
suffix: "",
modifier: "",
pattern: ".*",
modifier: "*",
pattern: "[^\\/#\\?]+?",
},
],
[
["/", ["/", ""]],
["/", ["/", undefined]],
["/route", ["/route", "route"]],
["/route/nested", ["/route/nested", "route/nested"]],
],
[
[{ 0: "" }, "/"],
[{ 0: "123" }, "/123"],
[{ 0: null }, ""],
[{ 0: "x" }, "/x"],
[{ 0: ["a", "b", "c"] }, "/a/b/c"],
],
],
[
"/+",
undefined,
[
"/",
{
name: 0,
prefix: "",
prefix: "/",
suffix: "",
modifier: "",
pattern: ".+",
modifier: "+",
pattern: "[^\\/#\\?]+?",
},
],
[
["/", null],
["/x", ["/x", "x"]],
["/route", ["/route", "route"]],
["/a/b/c", ["/a/b/c", "a/b/c"]],
],
[
[{ 0: "" }, null],
[{ 0: "x" }, "/x"],
[{ 0: "xyz" }, "/xyz"],
[{ 0: "route" }, "/route"],
[{ 0: ["a", "b", "c"] }, "/a/b/c"],
],
],
[
"/?",
undefined,
[
"/",
{
name: 0,
prefix: "",
prefix: "/",
suffix: "",
modifier: "",
pattern: ".?",
modifier: "?",
pattern: "[^\\/#\\?]+?",
},
],
[
["/", ["/", ""]],
["/", ["/", undefined]],
["/x", ["/x", "x"]],
["/route", null],
["/route", ["/route", "route"]],
],
[
[{ 0: "" }, "/"],
[{ 0: undefined }, ""],
[{ 0: "x" }, "/x"],
],
],
Expand Down
17 changes: 3 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,9 @@ export function parse(str: string, options: ParseOptions = {}): Token[] {
const char = tokens.tryConsume("CHAR");
const name = tokens.tryConsume("NAME");
const pattern = tokens.tryConsume("PATTERN");
const modifier = tokens.tryConsume("MODIFIER");

if (name || pattern) {
if (name || pattern || modifier) {
let prefix = char || "";

if (prefixes.indexOf(prefix) === -1) {
Expand All @@ -209,7 +210,7 @@ export function parse(str: string, options: ParseOptions = {}): Token[] {
prefix,
suffix: "",
pattern: pattern || defaultPattern,
modifier: tokens.tryConsume("MODIFIER") || "",
modifier: modifier || "",
});
continue;
}
Expand Down Expand Up @@ -244,18 +245,6 @@ export function parse(str: string, options: ParseOptions = {}): Token[] {
continue;
}

const modifier = tokens.tryConsume("MODIFIER");
if (modifier) {
result.push({
name: key++,
prefix: "",
suffix: "",
pattern: `.${modifier}`,
modifier: "",
});
continue;
}

tokens.consume("END");
break;
} while (true);
Expand Down

0 comments on commit 246ec9f

Please sign in to comment.