Skip to content

Commit 1cb25ad

Browse files
committed
sprint 2 implementation section is done in this commit
1 parent 2e6d362 commit 1cb25ad

8 files changed

Lines changed: 118 additions & 72 deletions

File tree

‎Sprint-2/implement/contains.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1-
function contains() {}
1+
function contains(keyInput, valueInput) {
2+
if (Array.isArray(keyInput) == true) {
3+
return false;
4+
} else {
5+
for (const key in keyInput) {
6+
if (key == valueInput) {
7+
return true;
8+
}
9+
}
10+
}
11+
return false;
12+
}
213

314
module.exports = contains;
Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,21 @@
11
const contains = require("./contains.js");
22

3-
/*
4-
Implement a function called contains that checks an object contains a
5-
particular property
3+
test("Give a contains with object and property and if the object contains the property returns true", () => {
4+
expect(contains({ a: 1, b: 2 }, "b")).toEqual(true);
5+
});
66

7-
E.g. contains({a: 1, b: 2}, 'a') // returns true
8-
as the object contains a key of 'a'
7+
test("Give a contains with empty object and when passed to contains it returns false", () => {
8+
expect(contains({}, "y")).toEqual(false);
9+
});
910

10-
E.g. contains({a: 1, b: 2}, 'c') // returns false
11-
as the object doesn't contains a key of 'c'
12-
*/
11+
test("Give a contains with object and property and if the object contains the property returns true", () => {
12+
expect(contains({ a: 1, b: 2 }, "a")).toEqual(true);
13+
});
1314

14-
// Acceptance criteria:
15+
test("Give a contains with object and property and if the property is non-existent returns false", () => {
16+
expect(contains({ a: 1, b: 2 }, "z")).toEqual(false);
17+
});
1518

16-
// Given a contains function
17-
// When passed an object and a property name
18-
// Then it should return true if the object contains the property, false otherwise
19-
20-
// Given an empty object
21-
// When passed to contains
22-
// Then it should return false
23-
test.todo("contains on empty object returns false");
24-
25-
// Given an object with properties
26-
// When passed to contains with an existing property name
27-
// Then it should return true
28-
29-
// Given an object with properties
30-
// When passed to contains with a non-existent property name
31-
// Then it should return false
32-
33-
// Given invalid parameters like an array
34-
// When passed to contains
35-
// Then it should return false or throw an error
19+
test("Give invalid parameter in this case array when passed to contains it returns false", () => {
20+
expect(contains(["a", 2, "b", 3, 2], 2)).toEqual(false);
21+
});

‎Sprint-2/implement/lookup.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1-
function createLookup() {
1+
function createLookup(countryCurrencyPairs) {
22
// implementation here
3+
4+
pairs = {};
5+
for (const [country, currency] of countryCurrencyPairs) {
6+
pairs[country] = currency;
7+
}
8+
return pairs;
39
}
410

511
module.exports = createLookup;

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

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,17 @@
11
const createLookup = require("./lookup.js");
22

3-
test.todo("creates a country currency code lookup for multiple codes");
4-
5-
/*
6-
7-
Create a lookup object of key value pairs from an array of code pairs
8-
9-
Acceptance Criteria:
10-
11-
Given
12-
- An array of arrays representing country code and currency code pairs
13-
e.g. [['US', 'USD'], ['CA', 'CAD']]
14-
15-
When
16-
- createLookup function is called with the country-currency array as an argument
17-
18-
Then
19-
- It should return an object where:
20-
- The keys are the country codes
21-
- The values are the corresponding currency codes
22-
23-
Example
24-
Given: [['US', 'USD'], ['CA', 'CAD']]
25-
26-
When
27-
createLookup(countryCurrencyPairs) is called
28-
29-
Then
30-
It should return:
31-
{
32-
'US': 'USD',
33-
'CA': 'CAD'
34-
}
35-
*/
3+
test("create a country currency code lookup for a single codes", () => {
4+
expect(createLookup([["UK", "GBP"]])).toEqual({ UK: "GBP" });
5+
});
6+
test("creates an empty country currency code returns empty lookup", () => {
7+
expect(createLookup([])).toEqual({});
8+
});
9+
test("creates a country currency code lookup for multiple codes", () => {
10+
expect(
11+
createLookup([
12+
["US", "USD"],
13+
["CA", "CAD"],
14+
["UK", "GBP"],
15+
])
16+
).toEqual({ US: "USD", CA: "CAD", UK: "GBP" });
17+
});

‎Sprint-2/implement/querystring.js‎

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,39 @@
11
function parseQueryString(queryString) {
22
const queryParams = {};
3-
if (queryString.length === 0) {
3+
if (!queryString) {
44
return queryParams;
55
}
6+
7+
// 1. Split on '&' to get individual key-value pairs
68
const keyValuePairs = queryString.split("&");
79

810
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
11+
// Skip completely empty segments from '&&' or trailing '&'
12+
if (!pair) continue;
13+
14+
// 2. Split on '=' separating key and value
15+
const eqIndex = pair.indexOf("=");
16+
let rawKey, rawValue;
17+
18+
if (eqIndex === -1) {
19+
// Key with no '=' (e.g. "key")
20+
rawKey = pair;
21+
rawValue = "";
22+
} else {
23+
rawKey = pair.slice(0, eqIndex);
24+
rawValue = pair.slice(eqIndex + 1);
25+
}
26+
27+
// 3. Replace '+' with ' ' first, then decode URL percent-encoded values
28+
const key = decodeURIComponent(rawKey.replace(/\+/g, " "));
29+
const value = decodeURIComponent(rawValue.replace(/\+/g, " "));
30+
31+
// 4. Handle identical keys (stretch goal)
32+
if (Object.hasOwn(queryParams, key)) {
33+
queryParams[key] = [].concat(queryParams[key], value);
34+
} else {
35+
queryParams[key] = value;
36+
}
1137
}
1238

1339
return queryParams;

‎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.js");
77

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

‎Sprint-2/implement/tally.js‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1-
function tally() {}
1+
function tally(items) {
2+
if (!Array.isArray(items)) {
3+
throw new Error("Invalid input!");
4+
}
5+
const result = {};
6+
for (const item of items) {
7+
result[item] = (result[item] || 0) + 1;
8+
}
9+
10+
return result;
11+
}
212

313
module.exports = tally;

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

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,41 @@ 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-
22+
test("given an array of items an object containing the counter for each item", () => {
23+
expect(tally(["a", "a"])).toEqual({ a: 2 });
24+
});
2325
// Given an empty array
2426
// When passed to tally
2527
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
28+
test("tally on an empty array returns an empty object", () => {
29+
expect(tally([])).toEqual({});
30+
});
2731

2832
// Given an array with duplicate items
2933
// When passed to tally
3034
// Then it should return counts for each unique item
31-
35+
test("given an array of items an object containing the counter for each item", () => {
36+
expect(tally(["a", "a", "c", "b", "b", "d"])).toEqual({
37+
a: 2,
38+
c: 1,
39+
b: 2,
40+
d: 1,
41+
});
42+
});
3243
// Given an invalid input like a string
3344
// When passed to tally
3445
// Then it should throw an error
46+
47+
/*
48+
try {
49+
tally("Invalid");
50+
console.error(
51+
"Test failed: Expected an error to be thrown for invalid input"
52+
);
53+
} catch (error) {
54+
console.log("Test passed: Error thrown for invalid input");
55+
}*/
56+
57+
test("tally on an empty array returns an empty object", () => {
58+
expect(() => tally("string")).toThrow("Invalid input!");
59+
});

0 commit comments

Comments
 (0)