From fb4d11d38f81e336a73a2ca0887e8105dd6d72ca Mon Sep 17 00:00:00 2001 From: Blake Embrey Date: Wed, 31 Jul 2024 15:20:52 -0700 Subject: [PATCH] Remove matches from tests --- scripts/redos.ts | 2 +- src/cases.spec.ts | 349 +++++++++------------------------------------- src/index.spec.ts | 4 +- src/index.ts | 73 ++++------ 4 files changed, 98 insertions(+), 330 deletions(-) diff --git a/scripts/redos.ts b/scripts/redos.ts index fe2c7ac..f83e52d 100644 --- a/scripts/redos.ts +++ b/scripts/redos.ts @@ -14,7 +14,7 @@ const TESTS = new Set(MATCH_TESTS.map((test) => test.path)); // ]; for (const path of TESTS) { - const { re } = match(path); + const { re } = match(path) as any; const result = checkSync(re.source, re.flags); if (result.status === "safe") { safe++; diff --git a/src/cases.spec.ts b/src/cases.spec.ts index 0f1cbf3..508b946 100644 --- a/src/cases.spec.ts +++ b/src/cases.spec.ts @@ -27,7 +27,6 @@ export interface MatchTestSet { options?: MatchOptions & ParseOptions; tests: Array<{ input: string; - matches: (string | undefined)[] | null; expected: Match; }>; } @@ -212,10 +211,9 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["/"], expected: { path: "/", params: {} }, }, - { input: "/route", matches: ["/"], expected: false }, + { input: "/route", expected: false }, ], }, { @@ -223,14 +221,12 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test", - matches: ["/test"], expected: { path: "/test", params: {} }, }, - { input: "/route", matches: null, expected: false }, - { input: "/test/route", matches: ["/test"], expected: false }, + { input: "/route", expected: false }, + { input: "/test/route", expected: false }, { input: "/test/", - matches: ["/test"], expected: false, }, ], @@ -240,14 +236,12 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test/", - matches: ["/test/"], expected: { path: "/test/", params: {} }, }, - { input: "/route", matches: null, expected: false }, - { input: "/test", matches: null, expected: false }, + { input: "/route", expected: false }, + { input: "/test", expected: false }, { input: "/test//", - matches: ["/test/"], expected: false, }, ], @@ -257,17 +251,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route", - matches: ["/route", "route"], expected: { path: "/route", params: { test: "route" } }, }, { input: "/route/", - matches: ["/route", "route"], expected: false, }, { input: "/route.json", - matches: ["/route.json", "route.json"], expected: { path: "/route.json", params: { test: "route.json" }, @@ -275,17 +266,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/route.json/", - matches: ["/route.json", "route.json"], expected: false, }, { input: "/route/test", - matches: ["/route", "route"], expected: false, }, { input: "/caf%C3%A9", - matches: ["/caf%C3%A9", "caf%C3%A9"], expected: { path: "/caf%C3%A9", params: { test: "café" }, @@ -293,7 +281,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/;,:@&=+$-_.!~*()", - matches: ["/;,:@&=+$-_.!~*()", ";,:@&=+$-_.!~*()"], expected: { path: "/;,:@&=+$-_.!~*()", params: { test: ";,:@&=+$-_.!~*()" }, @@ -301,7 +288,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/param%2523", - matches: ["/param%2523", "param%2523"], expected: { path: "/param%2523", params: { test: "param%23" }, @@ -321,10 +307,9 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test", - matches: ["/test"], expected: { path: "/test", params: {} }, }, - { input: "/TEST", matches: null, expected: false }, + { input: "/TEST", expected: false }, ], }, { @@ -335,10 +320,9 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/TEST", - matches: ["/TEST"], expected: { path: "/TEST", params: {} }, }, - { input: "/test", matches: null, expected: false }, + { input: "/test", expected: false }, ], }, @@ -353,32 +337,26 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test", - matches: ["/test"], expected: { path: "/test", params: {} }, }, { input: "/test/", - matches: ["/test"], expected: { path: "/test", params: {} }, }, { input: "/test////", - matches: ["/test"], expected: { path: "/test", params: {} }, }, { input: "/route/test", - matches: null, expected: false, }, { input: "/test/route", - matches: ["/test"], expected: { path: "/test", params: {} }, }, { input: "/route", - matches: null, expected: false, }, ], @@ -391,27 +369,22 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test", - matches: null, expected: false, }, { input: "/test/", - matches: ["/test/"], expected: { path: "/test/", params: {} }, }, { input: "/test//", - matches: ["/test/"], expected: { path: "/test/", params: {} }, }, { input: "/test/route", - matches: ["/test/"], expected: false, }, { input: "/route/test/deep", - matches: null, expected: false, }, ], @@ -424,17 +397,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route", - matches: ["/route", "route"], expected: { path: "/route", params: { test: "route" } }, }, { input: "/route/", - matches: ["/route", "route"], expected: { path: "/route", params: { test: "route" } }, }, { input: "/route.json", - matches: ["/route.json", "route.json"], expected: { path: "/route.json", params: { test: "route.json" }, @@ -442,7 +412,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/route.json/", - matches: ["/route.json", "route.json"], expected: { path: "/route.json", params: { test: "route.json" }, @@ -450,12 +419,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/route/test", - matches: ["/route", "route"], expected: { path: "/route", params: { test: "route" } }, }, { input: "/route.json/test", - matches: ["/route.json", "route.json"], expected: { path: "/route.json", params: { test: "route.json" }, @@ -463,7 +430,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/caf%C3%A9", - matches: ["/caf%C3%A9", "caf%C3%A9"], expected: { path: "/caf%C3%A9", params: { test: "café" }, @@ -479,27 +445,22 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route", - matches: null, expected: false, }, { input: "/route/", - matches: ["/route/", "route"], expected: { path: "/route/", params: { test: "route" } }, }, { input: "/route/test", - matches: ["/route/", "route"], expected: false, }, { input: "/route/test/", - matches: ["/route/", "route"], expected: false, }, { input: "/route//test", - matches: ["/route/", "route"], expected: { path: "/route/", params: { test: "route" } }, }, ], @@ -512,27 +473,22 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "", - matches: [""], expected: { path: "", params: {} }, }, { input: "/", - matches: [""], expected: { path: "", params: {} }, }, { input: "route", - matches: [""], expected: false, }, { input: "/route", - matches: [""], expected: { path: "", params: {} }, }, { input: "/route/", - matches: [""], expected: { path: "", params: {} }, }, ], @@ -546,17 +502,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route", - matches: ["/route", "route"], expected: { path: "/route", params: { test: "route" } }, }, { input: "", - matches: ["", undefined], expected: { path: "", params: {} }, }, { input: "/", - matches: ["", undefined], expected: false, }, ], @@ -566,17 +519,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/bar", - matches: ["/bar", undefined], expected: { path: "/bar", params: {} }, }, { input: "/foo/bar", - matches: ["/foo/bar", "foo"], expected: { path: "/foo/bar", params: { test: "foo" } }, }, { input: "/foo/bar/", - matches: ["/foo/bar", "foo"], expected: false, }, ], @@ -586,17 +536,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "-bar", - matches: ["-bar", undefined], expected: { path: "-bar", params: {} }, }, { input: "/foo-bar", - matches: ["/foo-bar", "foo"], expected: { path: "/foo-bar", params: { test: "foo" } }, }, { input: "/foo-bar/", - matches: ["/foo-bar", "foo"], expected: false, }, ], @@ -606,17 +553,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/-bar", - matches: ["/-bar", undefined], expected: { path: "/-bar", params: {} }, }, { input: "/foo-bar", - matches: ["/foo-bar", "foo"], expected: { path: "/foo-bar", params: { test: "foo" } }, }, { input: "/foo-bar/", - matches: ["/foo-bar", "foo"], expected: false, }, ], @@ -630,22 +574,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["", undefined], expected: false, }, { input: "//", - matches: ["", undefined], expected: false, }, { input: "/route", - matches: ["/route", "route"], expected: { path: "/route", params: { test: ["route"] } }, }, { input: "/some/basic/route", - matches: ["/some/basic/route", "some/basic/route"], expected: { path: "/some/basic/route", params: { test: ["some", "basic", "route"] }, @@ -658,22 +598,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "-bar", - matches: ["-bar", undefined], expected: { path: "-bar", params: {} }, }, { input: "/-bar", - matches: null, expected: false, }, { input: "/foo-bar", - matches: ["/foo-bar", "foo"], expected: { path: "/foo-bar", params: { test: ["foo"] } }, }, { input: "/foo/baz-bar", - matches: ["/foo/baz-bar", "foo/baz"], expected: { path: "/foo/baz-bar", params: { test: ["foo", "baz"] }, @@ -690,22 +626,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: null, expected: false, }, { input: "//", - matches: null, expected: false, }, { input: "/route", - matches: ["/route", "route"], expected: { path: "/route", params: { test: ["route"] } }, }, { input: "/some/basic/route", - matches: ["/some/basic/route", "some/basic/route"], expected: { path: "/some/basic/route", params: { test: ["some", "basic", "route"] }, @@ -718,22 +650,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "-bar", - matches: null, expected: false, }, { input: "/-bar", - matches: null, expected: false, }, { input: "/foo-bar", - matches: ["/foo-bar", "foo"], expected: { path: "/foo-bar", params: { test: ["foo"] } }, }, { input: "/foo/baz-bar", - matches: ["/foo/baz-bar", "foo/baz"], expected: { path: "/foo/baz-bar", params: { test: ["foo", "baz"] }, @@ -750,17 +678,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/123", - matches: ["/123", "123"], expected: { path: "/123", params: { test: "123" } }, }, { input: "/abc", - matches: null, expected: false, }, { input: "/123/abc", - matches: ["/123", "123"], expected: false, }, ], @@ -770,27 +695,22 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "-bar", - matches: null, expected: false, }, { input: "/-bar", - matches: null, expected: false, }, { input: "/abc-bar", - matches: null, expected: false, }, { input: "/123-bar", - matches: ["/123-bar", "123"], expected: { path: "/123-bar", params: { test: "123" } }, }, { input: "/123/456-bar", - matches: null, expected: false, }, ], @@ -800,17 +720,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["/", ""], expected: { path: "/", params: { test: "" } }, }, { input: "/route", - matches: ["/route", "route"], expected: { path: "/route", params: { test: "route" } }, }, { input: "/route/123", - matches: ["/route/123", "route/123"], expected: { path: "/route/123", params: { test: "route/123" }, @@ -818,7 +735,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/;,:@&=/+$-_.!/~*()", - matches: ["/;,:@&=/+$-_.!/~*()", ";,:@&=/+$-_.!/~*()"], expected: { path: "/;,:@&=/+$-_.!/~*()", params: { test: ";,:@&=/+$-_.!/~*()" }, @@ -831,17 +747,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/abc", - matches: ["/abc", "abc"], expected: { path: "/abc", params: { test: "abc" } }, }, { input: "/123", - matches: null, expected: false, }, { input: "/abc/123", - matches: ["/abc", "abc"], expected: false, }, ], @@ -851,17 +764,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/this", - matches: ["/this", "this"], expected: { path: "/this", params: { test: "this" } }, }, { input: "/that", - matches: ["/that", "that"], expected: { path: "/that", params: { test: "that" } }, }, { input: "/foo", - matches: null, expected: false, }, ], @@ -871,17 +781,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["", undefined], expected: false, }, { input: "/abc", - matches: ["/abc", "abc"], expected: { path: "/abc", params: { test: ["abc"] } }, }, { input: "/abc/abc", - matches: ["/abc/abc", "abc/abc"], expected: { path: "/abc/abc", params: { test: ["abc", "abc"] }, @@ -889,7 +796,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/xyz/xyz", - matches: ["/xyz/xyz", "xyz/xyz"], expected: { path: "/xyz/xyz", params: { test: ["xyz", "xyz"] }, @@ -897,7 +803,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/abc/xyz", - matches: ["/abc/xyz", "abc/xyz"], expected: { path: "/abc/xyz", params: { test: ["abc", "xyz"] }, @@ -905,7 +810,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/abc/xyz/abc/xyz", - matches: ["/abc/xyz/abc/xyz", "abc/xyz/abc/xyz"], expected: { path: "/abc/xyz/abc/xyz", params: { test: ["abc", "xyz", "abc", "xyz"] }, @@ -913,7 +817,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/xyzxyz", - matches: ["/xyz", "xyz"], expected: false, }, ], @@ -927,12 +830,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "test", - matches: ["test"], expected: { path: "test", params: {} }, }, { input: "/test", - matches: null, expected: false, }, ], @@ -942,17 +843,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "route", - matches: ["route", "route"], expected: { path: "route", params: { test: "route" } }, }, { input: "/route", - matches: null, expected: false, }, { input: "route/", - matches: ["route", "route"], expected: false, }, ], @@ -962,12 +860,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "test", - matches: ["test", "test"], expected: { path: "test", params: { test: "test" } }, }, { input: "", - matches: ["", undefined], expected: { path: "", params: {} }, }, ], @@ -977,22 +873,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "route/", - matches: ["route/", "route"], expected: { path: "route/", params: { test: ["route"] } }, }, { input: "/route", - matches: null, expected: false, }, { input: "", - matches: null, expected: false, }, { input: "foo/bar/", - matches: ["foo/bar/", "foo/bar"], expected: { path: "foo/bar/", params: { test: ["foo", "bar"] }, @@ -1009,12 +901,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test.json", - matches: ["/test.json"], expected: { path: "/test.json", params: {} }, }, { input: "/test", - matches: null, expected: false, }, ], @@ -1024,22 +914,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/.json", - matches: null, expected: false, }, { input: "/test.json", - matches: ["/test.json", "test"], expected: { path: "/test.json", params: { test: "test" } }, }, { input: "/route.json", - matches: ["/route.json", "route"], expected: { path: "/route.json", params: { test: "route" } }, }, { input: "/route.json.json", - matches: ["/route.json", "route"], expected: false, }, ], @@ -1049,7 +935,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route.json.json", - matches: ["/route.json.json", "route.json"], expected: { path: "/route.json.json", params: { test: "route.json" }, @@ -1066,12 +951,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test.html", - matches: ["/test.html", "html"], expected: { path: "/test.html", params: { format: "html" } }, }, { input: "/test", - matches: null, expected: false, }, ], @@ -1081,7 +964,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test.html.json", - matches: ["/test.html.json", "html", "json"], expected: { path: "/test.html.json", params: { format: "json" }, @@ -1089,7 +971,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/test.html", - matches: null, expected: false, }, ], @@ -1099,12 +980,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test", - matches: ["/test", undefined], expected: { path: "/test", params: { format: undefined } }, }, { input: "/test.html", - matches: ["/test.html", "html"], expected: { path: "/test.html", params: { format: "html" } }, }, ], @@ -1114,12 +993,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test", - matches: null, expected: false, }, { input: "/test.html", - matches: ["/test.html", "html"], expected: { path: "/test.html", params: { format: ["html"] }, @@ -1127,7 +1004,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/test.html.json", - matches: ["/test.html.json", "html.json"], expected: { path: "/test.html.json", params: { format: ["html", "json"] }, @@ -1140,12 +1016,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test", - matches: null, expected: false, }, { input: "/test.html", - matches: ["/test.html", "html"], expected: { path: "/test.html", params: { format: ["html"] }, @@ -1153,7 +1027,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/test.hbs.html", - matches: ["/test.hbs.html", "hbs.html"], expected: { path: "/test.hbs.html", params: { format: ["hbs", "html"] }, @@ -1170,7 +1043,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route.html", - matches: ["/route.html", "route", "html"], expected: { path: "/route.html", params: { test: "route", format: "html" }, @@ -1178,12 +1050,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/route", - matches: null, expected: false, }, { input: "/route.html.json", - matches: ["/route.html.json", "route", "html.json"], expected: { path: "/route.html.json", params: { test: "route", format: "html.json" }, @@ -1196,12 +1066,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route", - matches: ["/route", "route", undefined], expected: { path: "/route", params: { test: "route" } }, }, { input: "/route.json", - matches: ["/route.json", "route", "json"], expected: { path: "/route.json", params: { test: "route", format: "json" }, @@ -1209,7 +1077,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/route.json.html", - matches: ["/route.json.html", "route", "json.html"], expected: { path: "/route.json.html", params: { test: "route", format: "json.html" }, @@ -1222,7 +1089,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route.htmlz", - matches: ["/route.htmlz", "route", "html"], expected: { path: "/route.htmlz", params: { test: "route", format: "html" }, @@ -1230,7 +1096,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/route.html", - matches: null, expected: false, }, ], @@ -1244,17 +1109,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/123", - matches: ["/123", "123"], expected: { path: "/123", params: { "0": "123" } }, }, { input: "/abc", - matches: null, expected: false, }, { input: "/123/abc", - matches: ["/123", "123"], expected: false, }, ], @@ -1264,12 +1126,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["", undefined], expected: false, }, { input: "/123", - matches: ["/123", "123"], expected: { path: "/123", params: { "0": "123" } }, }, ], @@ -1279,7 +1139,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route(\\123\\)", - matches: ["/route(\\123\\)", "123\\"], expected: { path: "/route(\\123\\)", params: { "0": "123\\" }, @@ -1287,7 +1146,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/route(\\123)", - matches: null, expected: false, }, ], @@ -1297,22 +1155,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "", - matches: [""], expected: { path: "", params: {} }, }, { input: "/", - matches: [""], expected: false, }, { input: "/foo", - matches: [""], expected: false, }, { input: "/route", - matches: ["/route"], expected: { path: "/route", params: {} }, }, ], @@ -1322,12 +1176,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["/", ""], expected: { path: "/", params: { "0": "" } }, }, { input: "/login", - matches: ["/login", "login"], expected: { path: "/login", params: { "0": "login" } }, }, ], @@ -1341,12 +1193,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/testing", - matches: null, expected: false, }, { input: "/(testing)", - matches: ["/(testing)"], expected: { path: "/(testing)", params: {} }, }, ], @@ -1356,7 +1206,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/.+*?{}=^!:$[]|", - matches: ["/.+*?{}=^!:$[]|"], expected: { path: "/.+*?{}=^!:$[]|", params: {} }, }, ], @@ -1366,12 +1215,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test/u123", - matches: ["/test/u123", "u123", undefined], expected: { path: "/test/u123", params: { uid: "u123" } }, }, { input: "/test/c123", - matches: ["/test/c123", undefined, "c123"], expected: { path: "/test/c123", params: { cid: "c123" } }, }, ], @@ -1385,12 +1232,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/icon-240.png", - matches: ["/icon-240.png", "240"], expected: { path: "/icon-240.png", params: { res: "240" } }, }, { input: "/apple-icon-240.png", - matches: ["/apple-icon-240.png", "240"], expected: { path: "/apple-icon-240.png", params: { res: "240" }, @@ -1407,7 +1252,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/match/route", - matches: ["/match/route", "match", "route"], expected: { path: "/match/route", params: { foo: "match", bar: "route" }, @@ -1420,12 +1264,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/foo(test)/bar", - matches: ["/foo(test)/bar", "foo"], expected: { path: "/foo(test)/bar", params: { foo: "foo" } }, }, { input: "/foo/bar", - matches: null, expected: false, }, ], @@ -1435,7 +1277,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/endpoint/user", - matches: ["/endpoint/user", "endpoint", "user"], expected: { path: "/endpoint/user", params: { remote: "endpoint", user: "user" }, @@ -1443,7 +1284,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/endpoint/user-name", - matches: ["/endpoint/user-name", "endpoint", "user-name"], expected: { path: "/endpoint/user-name", params: { remote: "endpoint", user: "user-name" }, @@ -1451,7 +1291,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/foo.bar/user-name", - matches: ["/foo.bar/user-name", "foo.bar", "user-name"], expected: { path: "/foo.bar/user-name", params: { remote: "foo.bar", user: "user-name" }, @@ -1464,12 +1303,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route?", - matches: ["/route?", "route"], expected: { path: "/route?", params: { foo: "route" } }, }, { input: "/route", - matches: null, expected: false, }, ], @@ -1479,17 +1316,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/foobar", - matches: ["/foobar", "foo"], expected: { path: "/foobar", params: { foo: ["foo"] } }, }, { input: "/foo/bar", - matches: null, expected: false, }, { input: "/foo/barbar", - matches: null, expected: false, }, ], @@ -1499,12 +1333,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/foobaz", - matches: ["/foobaz", "foo"], expected: { path: "/foobaz", params: { pre: "foo" } }, }, { input: "/baz", - matches: ["/baz", undefined], expected: { path: "/baz", params: { pre: undefined } }, }, ], @@ -1514,7 +1346,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/hello(world)", - matches: ["/hello(world)", "hello", "world"], expected: { path: "/hello(world)", params: { foo: "hello", bar: "world" }, @@ -1522,7 +1353,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/hello()", - matches: null, expected: false, }, ], @@ -1532,7 +1362,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/hello(world)", - matches: ["/hello(world)", "hello", "world"], expected: { path: "/hello(world)", params: { foo: "hello", bar: "world" }, @@ -1540,7 +1369,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/hello()", - matches: ["/hello()", "hello", undefined], expected: { path: "/hello()", params: { foo: "hello", bar: undefined }, @@ -1553,12 +1381,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/video", - matches: ["/video", "video", undefined], expected: { path: "/video", params: { postType: "video" } }, }, { input: "/video+test", - matches: ["/video+test", "video", "+test"], expected: { path: "/video+test", params: { 0: "+test", postType: "video" }, @@ -1566,7 +1392,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/video+", - matches: ["/video", "video", undefined], expected: false, }, ], @@ -1576,12 +1401,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/-ext", - matches: null, expected: false, }, { input: "-ext", - matches: ["-ext", undefined, undefined], expected: { path: "-ext", params: { foo: undefined, bar: undefined }, @@ -1589,12 +1412,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/foo-ext", - matches: ["/foo-ext", "foo", undefined], expected: { path: "/foo-ext", params: { foo: "foo" } }, }, { input: "/foo/bar-ext", - matches: ["/foo/bar-ext", "foo", "bar"], expected: { path: "/foo/bar-ext", params: { foo: "foo", bar: "bar" }, @@ -1602,7 +1423,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/foo/-ext", - matches: null, expected: false, }, ], @@ -1612,12 +1432,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/foo-ext", - matches: ["/foo-ext", "foo", undefined], expected: { path: "/foo-ext", params: { required: "foo" } }, }, { input: "/foo/bar-ext", - matches: ["/foo/bar-ext", "foo", "bar"], expected: { path: "/foo/bar-ext", params: { required: "foo", optional: "bar" }, @@ -1625,7 +1443,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/foo/-ext", - matches: null, expected: false, }, ], @@ -1639,7 +1456,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/café", - matches: ["/café", "café"], expected: { path: "/café", params: { foo: "café" } }, }, ], @@ -1652,7 +1468,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/caf%C3%A9", - matches: ["/caf%C3%A9", "caf%C3%A9"], expected: { path: "/caf%C3%A9", params: { foo: "caf%C3%A9" }, @@ -1665,7 +1480,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/café", - matches: ["/café"], expected: { path: "/café", params: {} }, }, ], @@ -1678,7 +1492,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/caf%C3%A9", - matches: ["/caf%C3%A9"], expected: { path: "/caf%C3%A9", params: {} }, }, ], @@ -1695,7 +1508,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "example.com", - matches: ["example.com", "example"], expected: { path: "example.com", params: { domain: "example" }, @@ -1703,7 +1515,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "github.com", - matches: ["github.com", "github"], expected: { path: "github.com", params: { domain: "github" }, @@ -1719,7 +1530,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "mail.example.com", - matches: ["mail.example.com", "example"], expected: { path: "mail.example.com", params: { domain: "example" }, @@ -1727,7 +1537,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "mail.github.com", - matches: ["mail.github.com", "github"], expected: { path: "mail.github.com", params: { domain: "github" }, @@ -1743,12 +1552,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "mail.com", - matches: ["mail.com", undefined], expected: { path: "mail.com", params: { domain: undefined } }, }, { input: "mail.example.com", - matches: ["mail.example.com", "example"], expected: { path: "mail.example.com", params: { domain: "example" }, @@ -1756,7 +1563,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "mail.github.com", - matches: ["mail.github.com", "github"], expected: { path: "mail.github.com", params: { domain: "github" }, @@ -1772,12 +1578,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "example.com", - matches: ["example.com", "com"], expected: { path: "example.com", params: { ext: "com" } }, }, { input: "example.org", - matches: ["example.org", "org"], expected: { path: "example.org", params: { ext: "org" } }, }, ], @@ -1791,12 +1595,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "this is a test", - matches: ["this is"], expected: { path: "this is", params: {} }, }, { input: "this isn't", - matches: ["this is"], expected: false, }, ], @@ -1810,12 +1612,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "$x", - matches: ["$x", "x", undefined], expected: { path: "$x", params: { foo: "x" } }, }, { input: "$x$y", - matches: ["$x$y", "x", "y"], expected: { path: "$x$y", params: { foo: "x", bar: "y" } }, }, ], @@ -1825,12 +1625,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "$x", - matches: ["$x", "x"], expected: { path: "$x", params: { foo: ["x"] } }, }, { input: "$x$y", - matches: ["$x$y", "x$y"], expected: { path: "$x$y", params: { foo: ["x", "y"] } }, }, ], @@ -1840,12 +1638,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "name", - matches: ["name", undefined, undefined, undefined], expected: { path: "name", params: {} }, }, { input: "name/test", - matches: ["name/test", "test", undefined, undefined], expected: { path: "name/test", params: { attr1: "test" }, @@ -1853,7 +1649,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "name/1", - matches: ["name/1", "1", undefined, undefined], expected: { path: "name/1", params: { attr1: "1" }, @@ -1861,7 +1656,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "name/1-2", - matches: ["name/1-2", "1", "2", undefined], expected: { path: "name/1-2", params: { attr1: "1", attr2: "2" }, @@ -1869,7 +1663,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "name/1-2-3", - matches: ["name/1-2-3", "1", "2", "3"], expected: { path: "name/1-2-3", params: { attr1: "1", attr2: "2", attr3: "3" }, @@ -1877,12 +1670,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "name/foo-bar/route", - matches: ["name/foo-bar", "foo", "bar", undefined], expected: false, }, { input: "name/test/route", - matches: ["name/test", "test", undefined, undefined], expected: false, }, ], @@ -1892,12 +1683,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "name", - matches: ["name", undefined], expected: { path: "name", params: {} }, }, { input: "name/1", - matches: ["name/1", "1"], expected: { path: "name/1", params: { attrs: ["1"] }, @@ -1905,7 +1694,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "name/1-2", - matches: ["name/1-2", "1-2"], expected: { path: "name/1-2", params: { attrs: ["1", "2"] }, @@ -1913,7 +1701,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "name/1-2-3", - matches: ["name/1-2-3", "1-2-3"], expected: { path: "name/1-2-3", params: { attrs: ["1", "2", "3"] }, @@ -1921,12 +1708,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "name/foo-bar/route", - matches: ["name/foo-bar", "foo-bar"], expected: false, }, { input: "name/test/route", - matches: ["name/test", "test"], expected: false, }, ], @@ -1940,27 +1725,22 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/123", - matches: ["/123", "123"], expected: { path: "/123", params: { test: "123" } }, }, { input: "/abc", - matches: null, expected: false, }, { input: "/123/abc", - matches: ["/123", "123"], expected: false, }, { input: "/123.123", - matches: ["/123.123", "123.123"], expected: { path: "/123.123", params: { test: "123.123" } }, }, { input: "/123.abc", - matches: ["/123", "123"], expected: false, }, ], @@ -1970,12 +1750,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route", - matches: ["/route", "route"], expected: { path: "/route", params: { test: "route" } }, }, { input: "/login", - matches: null, expected: false, }, ], @@ -1989,12 +1767,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/user/123", - matches: ["/user/123", undefined, "123"], expected: { path: "/user/123", params: { user: "123" } }, }, { input: "/users/123", - matches: ["/users/123", "s", "123"], expected: { path: "/users/123", params: { 0: "s", user: "123" }, @@ -2007,12 +1783,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/user/123", - matches: ["/user/123", "123"], expected: { path: "/user/123", params: { user: "123" } }, }, { input: "/users/123", - matches: ["/users/123", "123"], expected: { path: "/users/123", params: { user: "123" } }, }, ], @@ -2026,7 +1800,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/files/hello/world.txt", - matches: ["/files/hello/world.txt", "hello/world", "txt"], expected: { path: "/files/hello/world.txt", params: { path: ["hello", "world"], ext: ["txt"] }, @@ -2034,7 +1807,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/files/hello/world.txt.png", - matches: ["/files/hello/world.txt.png", "hello/world", "txt.png"], expected: { path: "/files/hello/world.txt.png", params: { path: ["hello", "world"], ext: ["txt", "png"] }, @@ -2042,7 +1814,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/files/my/photo.jpg/gif", - matches: ["/files/my/photo.jpg", "my/photo", "jpg"], expected: false, }, ], @@ -2052,7 +1823,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/files/hello/world.txt", - matches: ["/files/hello/world.txt", "hello/world", "txt"], expected: { path: "/files/hello/world.txt", params: { path: ["hello", "world"], ext: "txt" }, @@ -2060,7 +1830,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/files/my/photo.jpg/gif", - matches: ["/files/my/photo.jpg", "my/photo", "jpg"], expected: false, }, ], @@ -2070,7 +1839,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "#/", - matches: ["#/", undefined], expected: { path: "#/", params: {} }, }, ], @@ -2080,7 +1848,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/foo/test1/test2", - matches: ["/foo/test1/test2", "test1/test2"], expected: { path: "/foo/test1/test2", params: { bar: ["test1", "test2"] }, @@ -2093,12 +1860,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/entity/foo", - matches: null, expected: false, }, { input: "/entity/foo/", - matches: ["/entity/foo/", "foo", undefined], expected: { path: "/entity/foo/", params: { id: "foo" } }, }, ], @@ -2108,22 +1873,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/test", - matches: null, expected: false, }, { input: "/test/", - matches: ["/test/", undefined], expected: { path: "/test/", params: {} }, }, { input: "/test/route", - matches: ["/test/route", "route"], expected: { path: "/test/route", params: { "0": ["route"] } }, }, { input: "/test/route/nested", - matches: ["/test/route/nested", "route/nested"], expected: { path: "/test/route/nested", params: { "0": ["route", "nested"] }, @@ -2140,17 +1901,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["/", undefined], expected: { path: "/", params: { "0": undefined } }, }, { input: "/route", - matches: ["/route", "route"], expected: { path: "/route", params: { "0": ["route"] } }, }, { input: "/route/nested", - matches: ["/route/nested", "route/nested"], expected: { path: "/route/nested", params: { "0": ["route", "nested"] }, @@ -2163,12 +1921,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["/", "/"], expected: { path: "/", params: { "0": ["", ""] } }, }, { input: "/test", - matches: ["/test", "/test"], expected: { path: "/test", params: { "0": ["", "test"] } }, }, ], @@ -2179,16 +1935,72 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["/", "/"], expected: { path: "/", params: { "0": "/" } }, }, { input: "/test", - matches: ["/test", "/test"], expected: { path: "/test", params: { "0": "/test" } }, }, ], }, + { + path: "/*.:ext", + tests: [ + { + input: "/test.html", + expected: { + path: "/test.html", + params: { "0": ["test"], ext: "html" }, + }, + }, + { + input: "/test.html/nested", + expected: false, + }, + ], + }, + { + path: "/*{.:ext}?", + tests: [ + { + input: "/test.html", + expected: { + path: "/test.html", + params: { "0": ["test.html"], ext: undefined }, + }, + }, + { + input: "/test.html/nested", + expected: { + params: { + "0": ["test.html", "nested"], + }, + path: "/test.html/nested", + }, + }, + ], + }, + { + path: "/*{.:ext}*", + tests: [ + { + input: "/test.html", + expected: { + path: "/test.html", + params: { "0": ["test.html"], ext: undefined }, + }, + }, + { + input: "/test.html/nested", + expected: { + params: { + "0": ["test.html", "nested"], + }, + path: "/test.html/nested", + }, + }, + ], + }, /** * Longer prefix. @@ -2198,12 +2010,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route", - matches: ["/route", "route", undefined], expected: { path: "/route", params: { foo: "route" } }, }, { input: "/route/test/again", - matches: ["/route/test/again", "route", "again"], expected: { path: "/route/test/again", params: { foo: "route", bar: "again" }, @@ -2220,12 +2030,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/", - matches: ["/", "test"], expected: { path: "/", params: { foo: ["test"] } }, }, { input: "/", - matches: ["/", "test>", params: { foo: ["test", "again"] }, @@ -2242,12 +2050,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "", - matches: ["", undefined, undefined], expected: { path: "", params: {} }, }, { input: "test/", - matches: ["test/", "test", undefined], expected: { path: "test/", params: { foo: "test" }, @@ -2255,7 +2061,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "a/b.", - matches: ["a/b.", "a", "b"], expected: { path: "a/b.", params: { foo: "a", bar: "b" } }, }, ], @@ -2265,22 +2070,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/abc", - matches: ["/abc", undefined], expected: { path: "/abc", params: {} }, }, { input: "/abcabc", - matches: ["/abc", undefined], expected: false, }, { input: "/abcabc123", - matches: ["/abcabc123", "123"], expected: { path: "/abcabc123", params: { foo: "123" } }, }, { input: "/abcabcabc123", - matches: ["/abcabcabc123", "abc123"], expected: { path: "/abcabcabc123", params: { foo: "abc123" }, @@ -2288,7 +2089,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/abcabcabc", - matches: ["/abcabcabc", "abc"], expected: { path: "/abcabcabc", params: { foo: "abc" } }, }, ], @@ -2298,22 +2098,18 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/abc", - matches: null, expected: false, }, { input: "/abcabc", - matches: null, expected: false, }, { input: "/abcabc123", - matches: null, expected: false, }, { input: "/acb", - matches: ["/acb", "acb", undefined], expected: { path: "/acb", params: { foo: "acb" }, @@ -2321,7 +2117,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/acbabc123", - matches: ["/acbabc123", "acb", "123"], expected: { path: "/acbabc123", params: { foo: "acb", bar: "123" }, @@ -2334,17 +2129,14 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/abc", - matches: null, expected: false, }, { input: "/abcabc", - matches: null, expected: false, }, { input: "/abcabc123", - matches: null, expected: false, }, ], @@ -2354,12 +2146,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/abc", - matches: ["/abc", "abc", undefined], expected: { path: "/abc", params: { foo: "abc" } }, }, { input: "/abc.txt", - matches: ["/abc.txt", "abc.txt", undefined], expected: { path: "/abc.txt", params: { foo: "abc.txt" } }, }, ], @@ -2369,7 +2159,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/route|world|", - matches: ["/route|world|", "world"], expected: { path: "/route|world|", params: { param: "world" }, @@ -2377,7 +2166,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/route||", - matches: null, expected: false, }, ], @@ -2387,7 +2175,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "/hello|world|", - matches: ["/hello|world|", "hello", "world"], expected: { path: "/hello|world|", params: { foo: "hello", bar: "world" }, @@ -2395,7 +2182,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "/hello||", - matches: null, expected: false, }, ], @@ -2405,12 +2191,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "x@y", - matches: ["x@y", "x", "y"], expected: { path: "x@y", params: { foo: "x", bar: "y" } }, }, { input: "x@", - matches: null, expected: false, }, ], @@ -2427,12 +2211,10 @@ export const MATCH_TESTS: MatchTestSet[] = [ tests: [ { input: "%25hello", - matches: ["%25hello", "hello", undefined], expected: { path: "%25hello", params: { foo: "hello" } }, }, { input: "%25hello%25world", - matches: ["%25hello%25world", "hello", "world"], expected: { path: "%25hello%25world", params: { foo: "hello", bar: "world" }, @@ -2440,7 +2222,6 @@ export const MATCH_TESTS: MatchTestSet[] = [ }, { input: "%25555%25222", - matches: ["%25555%25222", "555", "222"], expected: { path: "%25555%25222", params: { foo: "555", bar: "222" }, diff --git a/src/index.spec.ts b/src/index.spec.ts index b51dd9b..ef019c9 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -120,10 +120,8 @@ describe("path-to-regexp", () => { describe.each(MATCH_TESTS)( "match $path with $options", ({ path, options, tests }) => { - it.each(tests)("should match $input", ({ input, matches, expected }) => { + it.each(tests)("should match $input", ({ input, expected }) => { const fn = match(path, options); - - expect(exec(fn.re, input)).toEqual(matches); expect(fn(input)).toEqual(expected); }); }, diff --git a/src/index.ts b/src/index.ts index 9826439..2692df0 100644 --- a/src/index.ts +++ b/src/index.ts @@ -365,23 +365,23 @@ export function isOptional(key: Key) { * Convert a single token into a path building function. */ function keyToFunction( - token: Key, + key: Key, encode: Encode | false, ): (data: ParamData) => string { const encodeValue = encode || NOOP_VALUE; - const { prefix = "", suffix = "", separator = suffix + prefix } = token; + const { prefix = "", suffix = "", separator = suffix + prefix } = key; - if (encode && isRepeat(token)) { + if (encode && isRepeat(key)) { const stringify = (value: string, index: number) => { if (typeof value !== "string") { - throw new TypeError(`Expected "${token.name}/${index}" to be a string`); + throw new TypeError(`Expected "${key.name}/${index}" to be a string`); } return encodeValue(value); }; const compile = (value: unknown) => { if (!Array.isArray(value)) { - throw new TypeError(`Expected "${token.name}" to be an array`); + throw new TypeError(`Expected "${key.name}" to be an array`); } if (value.length === 0) return ""; @@ -389,37 +389,37 @@ function keyToFunction( return prefix + value.map(stringify).join(separator) + suffix; }; - if (isOptional(token)) { + if (isOptional(key)) { return (data): string => { - const value = data[token.name]; + const value = data[key.name]; if (value == null) return ""; return value.length ? compile(value) : ""; }; } return (data): string => { - const value = data[token.name]; + const value = data[key.name]; return compile(value); }; } const stringify = (value: unknown) => { if (typeof value !== "string") { - throw new TypeError(`Expected "${token.name}" to be a string`); + throw new TypeError(`Expected "${key.name}" to be a string`); } return prefix + encodeValue(value) + suffix; }; - if (isOptional(token)) { + if (isOptional(key)) { return (data): string => { - const value = data[token.name]; + const value = data[key.name]; if (value == null) return ""; return stringify(value); }; } return (data): string => { - const value = data[token.name]; + const value = data[key.name]; return stringify(value); }; } @@ -480,18 +480,7 @@ export type Match

= false | MatchResult

; /** * The match function takes a string and returns whether it matched the path. */ -export type MatchFunction

= (( - path: string, -) => Match

) & { re: RegExp }; - -const isEnd = (input: string, match: string) => input.length === match.length; -const isDelimiterOrEnd = - (delimiter: string) => (input: string, match: string) => { - return ( - match.length === input.length || - input.slice(match.length, match.length + delimiter.length) === delimiter - ); - }; +export type MatchFunction

= (path: string) => Match

; /** * Create path match function from `path-to-regexp` spec. @@ -501,18 +490,29 @@ export function $match

( options: MatchOptions = {}, ): MatchFunction

{ const { decode = decodeURIComponent, end = true } = options; - const re = tokensToRegexp(data, options); + const { delimiter } = data; + const keys: Key[] = []; + const flags = toFlags(options); + const sources = toRegExpSource(data, keys); + const re = new RegExp( + `^${sources.join("")}(?=${escape(delimiter)}|$)`, + flags, + ); + + const decoders = keys.map((key) => { + if (!decode) return NOOP_VALUE; - const decoders = re.keys.map((key) => { - if (decode && (key.modifier === "+" || key.modifier === "*")) { + if (isRepeat(key)) { const { prefix = "", suffix = "", separator = suffix + prefix } = key; return (value: string) => value.split(separator).map(decode); } - return decode || NOOP_VALUE; + return decode; }); - const validate = end ? isEnd : isDelimiterOrEnd(data.delimiter); + const isValid = end + ? (a: string, b: string) => a.length === b.length + : () => true; return Object.assign( function match(input: string) { @@ -520,13 +520,13 @@ export function $match

( if (!m) return false; const { 0: path } = m; - if (!validate(input, path)) return false; + if (!isValid(input, path)) return false; const params = Object.create(null); for (let i = 1; i < m.length; i++) { if (m[i] === undefined) continue; - const key = re.keys[i - 1]; + const key = keys[i - 1]; const decoder = decoders[i - 1]; params[key.name] = decoder(m[i]); } @@ -575,17 +575,6 @@ export interface Key { */ export type Token = string | Key; -/** - * Expose a function for taking tokens and returning a RegExp. - */ -function tokensToRegexp(data: TokenData, options: PathOptions) { - const flags = toFlags(options); - const keys: Key[] = []; - const sources = toRegExpSource(data, keys); - const regexp = new RegExp(`^${sources.join("")}`, flags); - return Object.assign(regexp, { keys }); -} - /** * Convert a token into a regexp string (re-used for path validation). */