Skip to content

Commit 827b052

Browse files
author
JorvanW
committed
added code and passed tests for querystring.test.js
1 parent 4c0d767 commit 827b052

2 files changed

Lines changed: 23 additions & 5 deletions

File tree

‎Sprint-2/implement/querystring.js‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,33 @@
1+
12
function parseQueryString(queryString) {
23
const queryParams = {};
34
if (queryString.length === 0) {
45
return queryParams;
56
}
6-
const keyValuePairs = queryString.split("&");
7-
7+
let keyValuePairs = queryString.split("&");
8+
9+
810
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
11+
if (!pair) continue; // continue is to skip empty strings
12+
let [key,...values] = pair.split("=");
13+
14+
key = decodeURIComponent(key.replace(/\+/g, " "));
15+
const value = decodeURIComponent(values.join("=").replace(/\+/g, " "));
16+
17+
18+
/* decodeURIComponent function decodes percent encoded characters
19+
"replace" swaps one character with another
20+
(/../) means the begining and end of a regex pattern (better for characters)
21+
'\+' is an escaped '+' because it has its own function in coding */
22+
1023
queryParams[key] = value;
24+
25+
26+
console.log(pair)
1127
}
1228

1329
return queryParams;
1430
}
1531

32+
1633
module.exports = parseQueryString;

‎Sprint-2/implement/querystring.test.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ test("should decode percent-encoded characters", () => {
3131
});
3232
});
3333

34-
test("should replace '+' by ' '", () => {
34+
test("should replace '+' by ' '", () => {
3535
expect(parseQueryString("full+name=John+Doe")).toEqual({
3636
"full name": "John Doe",
3737
});
3838
});
3939

40-
// Stretch exercise: Handling query strings that contain identical keys
40+
/* Stretch exercise: Handling query strings that contain identical keys
4141
4242
// Delete this test if you are not working on this optional case
4343
test("should store values of a key in an array when the key has 2 or more values", () => {
@@ -46,3 +46,4 @@ test("should store values of a key in an array when the key has 2 or more values
4646
foo: "bar",
4747
});
4848
});
49+
*/

0 commit comments

Comments
 (0)