Skip to content

Commit 3b91514

Browse files
Alex JamshidiAlex Jamshidi
authored andcommitted
completed querystring.js
1 parent f928482 commit 3b91514

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

Sprint-2/implement/querystring.js

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,40 @@ function parseQueryString(queryString) {
33
if (queryString.length === 0) {
44
return queryParams;
55
}
6+
7+
// Adds percentage-encoded characters
8+
queryString = changePEC(queryString)
9+
10+
// Replaces + with space
11+
queryString = queryString.replaceAll("+", " ")
12+
613
const keyValuePairs = queryString.split("&");
714

815
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
16+
if (pair !== "") {
17+
let index = pair.indexOf("=")
18+
if (!pair.includes("=")) {index = pair.length}
19+
const [key, value] = [pair.slice(0, index), pair.slice(index + 1)]
1020
queryParams[key] = value;
21+
}
22+
else {};
1123
}
1224

1325
return queryParams;
1426
}
1527

28+
// this function below can have the object PEC expanded to include all percentage-encoded characters
29+
// currently without adding this database, an unknown PEC could cause the code to get stuck in an infinite loop
30+
31+
function changePEC(string) {
32+
const PEC = {"%24": "$", "%2F": "/"}
33+
while (string.includes("%")) {
34+
const index = string.indexOf("%");
35+
const code = string.slice(index, index + 3);
36+
if (PEC[code]) {string = string.replace(code, PEC[code]);}
37+
else {break;}
38+
}
39+
return string
40+
}
41+
1642
module.exports = parseQueryString;

Sprint-2/implement/querystring.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@ test("should replace '+' by ' '", () => {
4040
// Stretch exercise: Handling query strings that contain identical keys
4141

4242
// Delete this test if you are not working on this optional case
43+
/*
4344
test("should store values of a key in an array when the key has 2 or more values", () => {
4445
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
4546
key: ["value1", "value2", "value3"],
4647
foo: "bar",
4748
});
4849
});
50+
*/

0 commit comments

Comments
 (0)