Skip to content

Commit 5bd4e7e

Browse files
committed
Fixed parseQueryString value parsing + filter empty pairs
1 parent 9e7a052 commit 5bd4e7e

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

Sprint-2/implement/querystring.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3-
if (queryString.length === 0) {
3+
4+
// Handle empty string or just "?"
5+
if (!queryString || queryString === "?") {
46
return queryParams;
57
}
6-
const keyValuePairs = queryString.split("&");
8+
9+
// Remove leading ? if present
10+
const cleaned = queryString.startsWith("?")
11+
? queryString.slice(1)
12+
: queryString;
13+
const keyValuePairs = cleaned.split("&").filter(Boolean);
714

815
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
16+
// Only split on the first =
17+
const equalIndex = pair.indexOf("=");
18+
19+
if (equalIndex === -1) {
20+
// key without = → value is empty string
21+
queryParams[pair] = "";
22+
} else {
23+
const key = pair.slice(0, equalIndex);
24+
const value = pair.slice(equalIndex + 1);
25+
queryParams[key] = value;
26+
}
1127
}
1228

1329
return queryParams;

0 commit comments

Comments
 (0)