Skip to content

Commit

Permalink
fix(core): req.params cant use capital letter
Browse files Browse the repository at this point in the history
  • Loading branch information
chizukicn committed Nov 29, 2023
1 parent 919dc58 commit 4a86768
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/core/src/shared/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ export function createRouteMatcher<T>(options: RouteMatcherOptions = {}): RouteM
if (!options.strictTrailingSlash) {
path = path.replace(/\/$/, "") || "/";
}
if (!options.caseSensitive) {
path = path.toLowerCase();
}
return path;
};

Expand All @@ -89,7 +86,11 @@ export function createRouteMatcher<T>(options: RouteMatcherOptions = {}): RouteM
type,
parent: currentNode
});
currentNode.children.set(segment, childNode);
if (type === NODE_TYPES.NORMAL && !options.caseSensitive) {
currentNode.children.set(segment.toLowerCase(), childNode);
} else {
currentNode.children.set(segment, childNode);
}
if (type === NODE_TYPES.PLACEHOLDER) {
const firstChar = segment[0];
if (firstChar === "*") {
Expand Down Expand Up @@ -156,13 +157,14 @@ export function createRouteMatcher<T>(options: RouteMatcherOptions = {}): RouteM
wildcardNode = currentNode;
wildcardParam = segments.slice(i).join("/");
}
const nextNode = currentNode.children.get(segments[i]);
const seg = segments[i];
const nextNode = currentNode.children.get(options.caseSensitive ? seg : seg.toLowerCase());
if (nextNode) {
currentNode = nextNode;
} else {
currentNode = currentNode.placeholderChildNode;
if (currentNode) {
params[currentNode.paramName!] = segments[i];
params[currentNode.paramName!] = seg;
paramsFound = true;
} else {
break;
Expand Down
4 changes: 2 additions & 2 deletions packages/core/test/matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ test("mock-matcher", async () => {
const matcher = createRouteMatcher<string>();

matcher.add("/hello/1", "get", "hello-1");
matcher.add("/hello/{abc}", "get", "hello-abc");
matcher.add("/hello/{userId}", "get", "hello-abc");
matcher.add("/hello/1", "post", "hello-post");
matcher.add("/hello/3", "all", "hello-3");

expect(matcher.match("/hello/1", "get")).toEqual(["hello-1", null]);
expect(matcher.match("/hello/2", "get")).toEqual(["hello-abc", { abc: "2" }]);
expect(matcher.match("/hello/2", "get")).toEqual(["hello-abc", { userId: "2" }]);
expect(matcher.match("/hello/1", "post")).toEqual(["hello-post", null]);
expect(matcher.match("/hello/3", "get")).toEqual(["hello-3", null]);
expect(matcher.match("/hello/3", "post")).toEqual(["hello-3", null]);
Expand Down

0 comments on commit 4a86768

Please sign in to comment.