Skip to content

Commit 2a4905f

Browse files
author
Enice-Codes
committed
fixed the logic of some excercise to pass the test
1 parent f4a3df7 commit 2a4905f

4 files changed

Lines changed: 50 additions & 23 deletions

File tree

‎Sprint-2/implement/querystring.js‎

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
1+
function decodeComponent(str) {
2+
return decodeURIComponent(str.replaceAll("+", " "));
3+
}
4+
15
function parseQueryString(queryString) {
2-
const queryParams = {};
36
if (queryString.length === 0) {
4-
return queryParams;
7+
return {};
58
}
6-
const keyValuePairs = queryString.split("&");
79

8-
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
10+
const params = {};
11+
const pairs = queryString.split("&").filter((pair) => pair !== "");
12+
13+
for (const pair of pairs) {
14+
const [rawKey, ...rawValueParts] = pair.split("=");
15+
const key = decodeComponent(rawKey);
16+
const value = decodeComponent(rawValueParts.join("="));
17+
18+
params[key] = key in params ? [].concat(params[key], value) : value;
1119
}
1220

13-
return queryParams;
21+
return params;
1422
}
1523

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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");
77

88
test("should parse values containing '='", () => {
99
expect(parseQueryString("equation=a=b-2")).toEqual({

‎Sprint-2/implement/tally.js‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1-
function tally() {}
2-
3-
module.exports = tally;
1+
function tally(arr) {
2+
if (!Array.isArray(arr)) {
3+
throw new Error("tally expects an array");
4+
}
5+
const counts = {};
6+
for (const item of arr) {
7+
counts[item] = (counts[item] || 0) + 1;
8+
}
9+
return counts;
10+
}
11+
module.exports = tally;

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,27 @@ const tally = require("./tally.js");
1919
// Given a function called tally
2020
// When passed an array of items
2121
// Then it should return an object containing the count for each unique item
22+
describe("tally on an array of items returns counts for each unique item", () => {
23+
// Given an empty array
24+
// When passed to tally
25+
// Then it should return an empty object
26+
test("tally on an empty array returns an empty object", () => {
27+
expect(tally([])).toEqual({});
28+
});
2229

23-
// Given an empty array
24-
// When passed to tally
25-
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
30+
// Given an array with duplicate items
31+
// When passed to tally
32+
// Then it should return counts for each unique item
33+
test("tally on an array with duplicate items returns counts for each unique item", () => {
34+
expect(tally(["a"])).toEqual({ a: 1 });
35+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
36+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
37+
});
2738

28-
// Given an array with duplicate items
29-
// When passed to tally
30-
// Then it should return counts for each unique item
31-
32-
// Given an invalid input like a string
33-
// When passed to tally
34-
// Then it should throw an error
39+
// Given an invalid input like a string
40+
// When passed to tally
41+
// Then it should throw an error
42+
test("tally throws an error for invalid input", () => {
43+
expect(() => tally("not an array")).toThrow();
44+
});
45+
});

0 commit comments

Comments
 (0)