Skip to content

Commit a169848

Browse files
authored
Enhance query string parsing functionality
Refactor parseQueryString to handle empty pairs and decode keys/values.
1 parent 37a2b47 commit a169848

1 file changed

Lines changed: 35 additions & 5 deletions

File tree

‎Sprint-2/implement/querystring.js‎

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,43 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3-
if (queryString.length === 0) {
3+
4+
if (queryString === "") {
45
return queryParams;
56
}
6-
const keyValuePairs = queryString.split("&");
77

8-
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
8+
const pairs = queryString.split("&");
9+
10+
for (const pair of pairs) {
11+
if (pair === "") {
12+
continue;
13+
}
14+
15+
const equalIndex = pair.indexOf("=");
16+
17+
let key;
18+
let value;
19+
20+
if (equalIndex === -1) {
21+
key = pair;
22+
value = "";
23+
} else {
24+
key = pair.slice(0, equalIndex);
25+
value = pair.slice(equalIndex + 1);
26+
}
27+
28+
key = key.replace(/\+/g, " ");
29+
value = value.replace(/\+/g, " ");
30+
31+
key = decodeURIComponent(key);
32+
value = decodeURIComponent(value);
33+
34+
if (queryParams[key] === undefined) {
35+
queryParams[key] = value;
36+
} else if (Array.isArray(queryParams[key])) {
37+
queryParams[key].push(value);
38+
} else {
39+
queryParams[key] = [queryParams[key], value];
40+
}
1141
}
1242

1343
return queryParams;

0 commit comments

Comments
 (0)