Skip to content

Commit c27db66

Browse files
committed
querystring function and testing it
1 parent e431148 commit c27db66

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

Sprint-2/implement/querystring.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,21 @@ function parseQueryString(queryString) {
66
const keyValuePairs = queryString.split("&");
77

88
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
9+
if (!pair) {
10+
continue;
11+
} else if (pair.includes("=")) {
12+
let i = pair.indexOf("=");
13+
let key = pair.slice(0, i).trim();
14+
let value = pair.slice(i + 1).trim();
15+
queryParams[key] = value;
16+
} else {
17+
queryParams[pair.trim()] = "";
18+
}
1119
}
1220

1321
return queryParams;
1422
}
23+
const result = parseQueryString("more=1=1+0&equation = x = y + 1&goga");
24+
console.log(result);
1525

1626
module.exports = parseQueryString;

Sprint-2/implement/querystring.test.js

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,54 @@
33
// Below is one test case for an edge case the implementation doesn't handle well.
44
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.
55

6-
const parseQueryString = require("./querystring.js")
6+
const parseQueryString = require("./querystring.js");
77

8-
test("parses querystring values containing =", () => {
9-
expect(parseQueryString("equation=x=y+1")).toEqual({
10-
"equation": "x=y+1",
8+
describe("edge cases for parseQueryString function", () => {
9+
test("parses querystring values containing =", () => {
10+
expect(parseQueryString("equation=x=y+1")).toEqual({
11+
equation: "x=y+1",
12+
});
13+
});
14+
15+
test("parses querystring values containing '=' and additional whitespaces around '=' sign", () => {
16+
expect(parseQueryString("equation = x=y+1")).toEqual({
17+
equation: "x=y+1",
18+
});
19+
});
20+
21+
test("parses empty querystring", () => {
22+
expect(parseQueryString("")).toEqual({});
23+
});
24+
25+
test("parses querystring with duplicate keys", () => {
26+
expect(parseQueryString("equation=x&equation=y+1")).toEqual({
27+
equation: "x",
28+
equation: "y+1",
29+
});
30+
});
31+
32+
test("parses querystring with leading or trailing &s", () => {
33+
expect(parseQueryString("&equation=x&")).toEqual({
34+
equation: "x",
35+
});
36+
});
37+
38+
test("parses querystring with missing key", () => {
39+
expect(parseQueryString("=x")).toEqual({
40+
"": "x",
41+
});
42+
});
43+
44+
test("parses querystring with missing value", () => {
45+
expect(parseQueryString("equation=")).toEqual({
46+
equation: "",
47+
});
48+
});
49+
50+
test("parses querystring with missing '='", () => {
51+
expect(parseQueryString("equation&gh5")).toEqual({
52+
equation: "",
53+
gh5: "",
54+
});
1155
});
1256
});

0 commit comments

Comments
 (0)