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 all commits
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
7 changes: 3 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export function hasTrailingSlash(
/**
* 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
Loading