Skip to content

Commit 6cd1fcd

Browse files
thijswpi0
andauthored
fix(withoutTrailingSlash): consider qurry param (#219)
* fix: consider scenario where / is part of query string in withoutTrailingSlash() * revert change in `hasTrailingSlash` --------- Co-authored-by: Pooya Parsa <[email protected]>
1 parent 69e26f8 commit 6cd1fcd

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

src/utils.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export function hasTrailingSlash(
9393
/**
9494
* Removes trailing slash from the URL or pathname.
9595
*
96-
* 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.
96+
* 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.
9797
*
9898
* @example
9999
*
@@ -123,10 +123,9 @@ export function withoutTrailingSlash(
123123
fragment = input.slice(fragmentIndex);
124124
}
125125
const [s0, ...s] = path.split("?");
126+
const cleanPath = s0.endsWith("/") ? s0.slice(0, -1) : s0;
126127
return (
127-
(s0.slice(0, -1) || "/") +
128-
(s.length > 0 ? `?${s.join("?")}` : "") +
129-
fragment
128+
(cleanPath || "/") + (s.length > 0 ? `?${s.join("?")}` : "") + fragment
130129
);
131130
}
132131

test/trailing-slash.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ describe("withoutTrailingSlash, queryParams: false", () => {
5757
"foo?123": "foo?123",
5858
"foo/?123": "foo/?123",
5959
"foo/?123#abc": "foo/?123#abc",
60+
"foo/?k=v": "foo/?k=v",
61+
"foo/?k=/": "foo/?k=",
6062
};
6163

6264
for (const input in tests) {
@@ -81,6 +83,10 @@ describe("withoutTrailingSlash, queryParams: true", () => {
8183
"foo?123": "foo?123",
8284
"foo/?123": "foo?123",
8385
"foo/?123#abc": "foo?123#abc",
86+
"foo/?k=123": "foo?k=123",
87+
"foo?k=/": "foo?k=/",
88+
"foo/?k=/": "foo?k=/",
89+
"foo/?k=/&x=y#abc": "foo?k=/&x=y#abc",
8490
"/a/#abc": "/a#abc",
8591
"/#abc": "/#abc",
8692
};

0 commit comments

Comments
 (0)