Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(toRouteMatcher): respect non strict trailing slash #91

Merged
merged 3 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ export interface RouteMatcher {

export function toRouteMatcher(router: RadixRouter): RouteMatcher {
const table = _routerNodeToTable("", router.ctx.rootNode);
return _createMatcher(table);
return _createMatcher(table, router.ctx.options.strictTrailingSlash);
}

function _createMatcher(table: RouteTable): RouteMatcher {
function _createMatcher(
table: RouteTable,
strictTrailingSlash?: boolean,
): RouteMatcher {
return {
ctx: { table },
matchAll: (path) => _matchRoutes(path, table),
matchAll: (path: string) => _matchRoutes(path, table, strictTrailingSlash),
} satisfies RouteMatcher;
}

Expand Down Expand Up @@ -83,7 +86,16 @@ export function createMatcherFromExport(
return _createMatcher(_createTableFromExport(matcherExport));
}

function _matchRoutes(path: string, table: RouteTable): RadixNodeData[] {
function _matchRoutes(
path: string,
table: RouteTable,
strictTrailingSlash?: boolean,
): RadixNodeData[] {
// By default trailing slashes are not strict
if (strictTrailingSlash !== true && path.endsWith("/")) {
path = path.slice(0, -1) || "/";
}

// Order should be from less specific to most specific
const matches = [];

Expand Down
34 changes: 34 additions & 0 deletions tests/matcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe("Route matcher", function () {
"/foo/baz",
"/foo/baz/**",
"/foo/*/sub",
"/without-trailing",
"/with-trailing/",
]);

const router = createRouter({ routes });
Expand Down Expand Up @@ -88,6 +90,12 @@ describe("Route matcher", function () {
"/foo/baz" => {
"pattern": "/foo/baz",
},
"/without-trailing" => {
"pattern": "/without-trailing",
},
"/with-trailing" => {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
"pattern": "/with-trailing/",
},
},
"wildcard": Map {
"/foo" => {
Expand Down Expand Up @@ -142,6 +150,26 @@ describe("Route matcher", function () {
`);
});

it("trailing slash", () => {
// Defined with trailing slash
expect(_match("/with-trailing")).to.toMatchInlineSnapshot(`
[
"/with-trailing/",
]
`);
expect(_match("/with-trailing")).toMatchObject(_match("/with-trailing/"));

// Defined without trailing slash
expect(_match("/without-trailing")).to.toMatchInlineSnapshot(`
[
"/without-trailing",
]
`);
expect(_match("/without-trailing")).toMatchObject(
_match("/without-trailing/"),
);
});

it("can be exported", () => {
const jsonData = exportMatcher(matcher);
expect(jsonData).toMatchInlineSnapshot(`
Expand Down Expand Up @@ -173,6 +201,12 @@ describe("Route matcher", function () {
"/foo/baz": {
"pattern": "/foo/baz",
},
"/with-trailing": {
"pattern": "/with-trailing/",
},
"/without-trailing": {
"pattern": "/without-trailing",
},
},
"wildcard": {
"/foo": {
Expand Down