Skip to content

Commit b44572c

Browse files
committed
implemented querystring function and wrote test for it with edge cases well
1 parent 9302152 commit b44572c

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

Sprint-2/implement/querystring.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,30 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3+
34
if (queryString.length === 0) {
45
return queryParams;
56
}
7+
68
const keyValuePairs = queryString.split("&");
79

810
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
11+
const index = pair.indexOf("=");
12+
13+
let key;
14+
let value;
15+
16+
if (index === -1) {
17+
key = pair;
18+
value = "";
19+
} else {
20+
key = pair.slice(0, index);
21+
value = pair.slice(index + 1);
22+
}
23+
1024
queryParams[key] = value;
1125
}
1226

1327
return queryParams;
1428
}
1529

16-
module.exports = parseQueryString;
30+
module.exports = parseQueryString;

Sprint-2/implement/querystring.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,28 @@ test("parses querystring values containing =", () => {
1010
"equation": "x=y+1",
1111
});
1212
});
13+
14+
test("parses querystring values containing &", () => {
15+
expect(parseQueryString("equation=x&y=1")).toEqual({
16+
"equation": "x",
17+
"y": "1",
18+
});
19+
});
20+
21+
test("parses querystring values containing & and =", () => {
22+
expect(parseQueryString("equation=x&y=1&formula=a=b+c")).toEqual({
23+
"equation": "x",
24+
"y": "1",
25+
"formula": "a=b+c",
26+
});
27+
});
28+
29+
test("parses empty querystring", () => {
30+
expect(parseQueryString("")).toEqual({});
31+
});
32+
33+
test("parses querystring with no value", () => {
34+
expect(parseQueryString("key")).toEqual({
35+
"key": "",
36+
});
37+
});

0 commit comments

Comments
 (0)