Skip to content

Commit 8730b63

Browse files
committed
fix errors
1 parent 36fd665 commit 8730b63

5 files changed

Lines changed: 1847 additions & 138 deletions

File tree

‎Sprint-2/implement/querystring.js‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3+
if (queryString === "") {
4+
return queryString;
5+
}
36
if (queryString.length === 0) {
47
return queryParams;
58
}
6-
const keyValuePairs = queryString.split("&");
9+
10+
const keyValuePairs = queryString.split("&").filter((pair) => pair !== "");
711

812
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
13+
const indexFirstEqual = pair.indexOf("=");
14+
15+
let key;
16+
let value;
17+
18+
if (indexFirstEqual === -1) {
19+
key = pair;
20+
value = "";
21+
} else {
22+
key = pair.slice(0, indexFirstEqual);
23+
value = pair.slice(indexFirstEqual + 1);
24+
}
25+
26+
key = decodeURIComponent(key.replace(/\+/g, " "));
27+
value = decodeURIComponent(value.replace(/\+/g, " "));
28+
1029
queryParams[key] = value;
1130
}
1231

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

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,69 @@
33
// Below are some test cases the implementation doesn't handle well.
44
// Fix the implementation for these tests, 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

88
test("should parse values containing '='", () => {
9-
expect(parseQueryString("equation=a=b-2")).toEqual({
9+
const input = "equation=a=b-2";
10+
const currentOutput = parseQueryString(input);
11+
const targetOutput = {
1012
equation: "a=b-2",
11-
});
13+
};
14+
15+
expect(currentOutput).toStrictEqual(targetOutput);
1216
});
1317

1418
test("should ignore empty key-value pairs", () => {
15-
expect(parseQueryString("key1=value1&&key2=value2&")).toEqual({
19+
const input = "key1=value1&&key2=value2&";
20+
const currentOutput = parseQueryString(input);
21+
const targetOutput = {
1622
key1: "value1",
1723
key2: "value2",
18-
});
24+
};
25+
expect(currentOutput).toEqual(targetOutput);
1926
});
20-
2127
test("should accept empty string as key or as value", () => {
22-
expect(parseQueryString("=value")).toEqual({ "": "value" });
23-
expect(parseQueryString("key")).toEqual({ key: "" });
24-
expect(parseQueryString("key=")).toEqual({ key: "" });
25-
expect(parseQueryString("=")).toEqual({ "": "" });
28+
expect(parseQueryString("=value")).toEqual({
29+
"": "value",
30+
});
31+
32+
expect(parseQueryString("key=")).toEqual({
33+
key: "",
34+
});
35+
36+
expect(parseQueryString("=")).toEqual({
37+
"": "",
38+
});
2639
});
2740

41+
// test("should accept empty string as key or as value", () => {
42+
// const currentOutput = parseQueryString(input);
43+
// }
44+
// expect(currentOutput).toEqual(targetOutput);
45+
// expect(parseQueryString("=value")).toEqual({ "": "value" });
46+
// expect(parseQueryString("key")).toEqual({ key: "" });
47+
// expect(parseQueryString("key=")).toEqual({ key: "" });
48+
// expect(parseQueryString("=")).toEqual({ "": "" });
49+
2850
test("should decode percent-encoded characters", () => {
2951
expect(parseQueryString("%24half=1%2F2")).toEqual({
3052
$half: "1/2",
3153
});
3254
});
3355

3456
test("should replace '+' by ' '", () => {
35-
expect(parseQueryString("full+name=John+Doe")).toEqual({
36-
"full name": "John Doe",
37-
});
57+
const input = "full+name=John+Doe";
58+
const currentOutput = parseQueryString(input);
59+
const expectedOutput = { "full name": "John Doe" };
60+
expect(currentOutput).toEqual(expectedOutput);
3861
});
3962

4063
// Stretch exercise: Handling query strings that contain identical keys
4164

4265
// Delete this test if you are not working on this optional case
43-
test("should store values of a key in an array when the key has 2 or more values", () => {
44-
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
45-
key: ["value1", "value2", "value3"],
46-
foo: "bar",
47-
});
48-
});
66+
// test("should store values of a key in an array when the key has 2 or more values", () => {
67+
// const input = "key=value1&key=value2&key=value3&foo=bar";
68+
// const currentOutput = parseQueryString(input);
69+
// const expectedOutput = { key: ["value1", "value2", "value3"], foo: "bar" };
70+
// expect(currentOutput).toEqual(expectedOutput);
71+
// });

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,24 @@ const tally = require("./tally.js");
2323
// Given an empty array
2424
// When passed to tally
2525
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
26+
test("should return an empty oject when given an empty array", () => {
27+
expect(tally([])).toEqual({});
28+
});
2729

2830
// Given an array with duplicate items
2931
// When passed to tally
3032
// Then it should return counts for each unique item
3133

32-
// Given an invalid input like a string
33-
// When passed to tally
34-
// Then it should throw an error
34+
test("should return counts for each unique item", () => {
35+
expect(tally(["a", "b", "a", "b"])).toEqual({
36+
a: 2,
37+
b: 2,
38+
});
39+
});
40+
// given an invalid input like a string
41+
//when passed to tally
42+
//then it should throw an error
43+
44+
test("should throw an error when passed invalid input", () => {
45+
expect(() => tally("")).toThrow(Error);
46+
});

0 commit comments

Comments
 (0)