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(withoutTrailingSlash): consider qurry param #219

Merged
merged 2 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,15 @@ export function hasTrailingSlash(
respectQueryAndFragment?: boolean
): boolean {
if (!respectQueryAndFragment) {
return input.endsWith("/");
return !input.includes("?") && input.endsWith("/");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(notice that by default, we do not handle query. we probably don't want to change logic in this line)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see that query is not handled by default, but what to do with these cases?

because, currently, all these calls return true:

hasTrailingSlash('/a?b=/', false)
hasTrailingSlash('/a/?b=/', false)
hasTrailingSlash('/a?b=/', true)
hasTrailingSlash('/a/?b=/', true)

Copy link
Member

@pi0 pi0 Mar 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking, without this change, only failing test is withoutTrailingSlash('foo/?k=/', false /* no respect */). (test in L88 still passes)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have revert it for now since change on hasProcol like this is risky to introduce behavior changes.

}
return TRAILING_SLASH_RE.test(input);
}

/**
* Removes trailing slash from the URL or pathname.
*
* If second argument is is true, it will only remove the trailing slash if it's not part of the query or fragment with cost of more expensive operations.
* If second argument is true, it will only remove the trailing slash if it's not part of the query or fragment with cost of more expensive operations.
*
* @example
*
Expand Down Expand Up @@ -122,10 +122,9 @@ export function withoutTrailingSlash(
fragment = input.slice(fragmentIndex);
}
const [s0, ...s] = path.split("?");
const cleanPath = s0.endsWith("/") ? s0.slice(0, -1) : s0;
return (
(s0.slice(0, -1) || "/") +
(s.length > 0 ? `?${s.join("?")}` : "") +
fragment
(cleanPath || "/") + (s.length > 0 ? `?${s.join("?")}` : "") + fragment
);
}

Expand Down
6 changes: 6 additions & 0 deletions test/trailing-slash.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe("withoutTrailingSlash, queryParams: false", () => {
"foo?123": "foo?123",
"foo/?123": "foo/?123",
"foo/?123#abc": "foo/?123#abc",
"foo/?k=v": "foo/?k=v",
"foo/?k=/": "foo/?k=/",
};

for (const input in tests) {
Expand All @@ -81,6 +83,10 @@ describe("withoutTrailingSlash, queryParams: true", () => {
"foo?123": "foo?123",
"foo/?123": "foo?123",
"foo/?123#abc": "foo?123#abc",
"foo/?k=123": "foo?k=123",
"foo?k=/": "foo?k=/",
"foo/?k=/": "foo?k=/",
"foo/?k=/&x=y#abc": "foo?k=/&x=y#abc",
"/a/#abc": "/a#abc",
"/#abc": "/#abc",
};
Expand Down