From 762bc6b0d637874e4dba04e9d21e4ce9c4c1bcf3 Mon Sep 17 00:00:00 2001 From: Artem Zakharchenko Date: Fri, 11 Feb 2022 04:28:54 +0100 Subject: [PATCH] Fix invalid matching of `:name*` parameter (#261) --- src/index.spec.ts | 33 +++++++++++++++++++++++++++++++++ src/index.ts | 6 +++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/src/index.spec.ts b/src/index.spec.ts index 168a410..a4cae6b 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -2593,6 +2593,39 @@ const TESTS: Test[] = [ [{ foo: "#" }, null], ], ], + /** + * https://github.com/pillarjs/path-to-regexp/issues/260 + */ + [ + ":name*", + undefined, + [ + { + name: "name", + prefix: "", + suffix: "", + modifier: "*", + pattern: "[^\\/#\\?]+?", + }, + ], + [["foobar", ["foobar", "foobar"]]], + [[{ name: "foobar" }, "foobar"]], + ], + [ + ":name+", + undefined, + [ + { + name: "name", + prefix: "", + suffix: "", + modifier: "+", + pattern: "[^\\/#\\?]+?", + }, + ], + [["foobar", ["foobar", "foobar"]]], + [[{ name: "foobar" }, "foobar"]], + ], ]; /** diff --git a/src/index.ts b/src/index.ts index 05c8464..a419ae6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -563,7 +563,11 @@ export function tokensToRegexp( route += `(?:${prefix}(${token.pattern})${suffix})${token.modifier}`; } } else { - route += `(${token.pattern})${token.modifier}`; + if (token.modifier === "+" || token.modifier === "*") { + route += `((?:${token.pattern})${token.modifier})`; + } else { + route += `(${token.pattern})${token.modifier}`; + } } } else { route += `(?:${prefix}${suffix})${token.modifier}`;