Skip to content

Commit 9c07c98

Browse files
committed
implement queryString()
1 parent 0b6fc65 commit 9c07c98

1 file changed

Lines changed: 29 additions & 4 deletions

File tree

‎Sprint-2/implement/querystring.js‎

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,37 @@ function parseQueryString(queryString) {
33
if (queryString.length === 0) {
44
return queryParams;
55
}
6-
const keyValuePairs = queryString.split("&");
76

8-
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
7+
//replace initial "?" if present
8+
if (queryString.startsWith("?")) {
9+
queryString = queryString.replace("?", "");
1110
}
11+
//replace encoded characters
12+
queryString = decodeURIComponent(queryString);
13+
14+
//replace "+" with " "
15+
queryString = queryString.replaceAll("+", " ");
16+
17+
const keyValuePairs = queryString.split("&");
18+
19+
//filter out empty strings from the keyValuePairs array of strings
20+
let filteredKeyValuePairs = keyValuePairs.filter(
21+
(keyValuePair) => keyValuePair.length > 0
22+
);
23+
24+
//assign key value pairs created by separating on the first "=" sign
25+
filteredKeyValuePairs.forEach((str) => {
26+
//if no "=", then the string should be the key
27+
if (!str.includes("=")) {
28+
const key = str;
29+
queryParams[key] = "";
30+
} else {
31+
const indexOfFirstEqual = str.indexOf("=");
32+
const key = str.slice(0, indexOfFirstEqual);
33+
const value = str.slice(indexOfFirstEqual + 1);
34+
queryParams[key] = value;
35+
}
36+
});
1237

1338
return queryParams;
1439
}

0 commit comments

Comments
 (0)