Skip to content

Commit 7cb9ffa

Browse files
committed
Pass all tests on querystring.js
1 parent 6499f68 commit 7cb9ffa

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

‎Sprint-2/implement/querystring.js‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,36 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3+
4+
// return if input is 0
35
if (queryString.length === 0) {
46
return queryParams;
57
}
8+
9+
// replace all + by ' '
10+
queryString = queryString.replaceAll("+", " ");
11+
12+
// split by &
613
const keyValuePairs = queryString.split("&");
714

815
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
16+
if (pair.length > 0) {
17+
const parts = pair.split("=");
18+
// first part as the key
19+
const key = decodeURIComponent(parts[0]);
20+
// second to last parts as the value
21+
const value = decodeURIComponent(parts.slice(1).join("="));
22+
23+
// if no key then assign new value
24+
if (!(key in queryParams)) {
25+
queryParams[key] = value;
26+
// if array then push a new value to it
27+
} else if (Array.isArray(queryParams[key])) {
28+
queryParams[key].push(value);
29+
// if not array then turn into array with old and new values
30+
} else {
31+
queryParams[key] = [queryParams[key], value];
32+
}
33+
}
1134
}
1235

1336
return queryParams;

0 commit comments

Comments
 (0)