-
-
Notifications
You must be signed in to change notification settings - Fork 286
Cape Town | 26-ITP-Jan | Pretty Taruvinga | Sprint 2 | Data Groups #1109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
53fa90f
implement correct median calculation
7b40348
Access houseNumber correctly from address object
c04e2dc
Use Object.values to loop through author object
59fff2a
display ingredients correctly instead of object
bbdc491
Add contains function to check if value exists in array
904055c
implement contains function for object property check
7f28769
implement contains test cases
e6ec9d4
Made it look clean
067f3e1
Implement createLookup function for country-currency pairs
5b88737
set up jest and add test script
92c0f04
1. add createLookup test cases for multiple and empty inputs
12f79f0
Correctly parse query strings with '=' in values and edge cases"
3c7d554
Implement tally function with error handling and tests"
78d5e44
Correctly invert object keys and values using bracket notation"
4c3daf6
Revert to main
db6200c
Merge branch 'main' into sprint-2
Pretty548 c3b7bf0
Merge branch 'sprint-2' of https://github.com/Pretty548/Module-Data-G…
1b5a7e8
removed the package.json
1f43925
Fix contains to return false for non-object inputs
84d5a4e
Use Object.hasOwn and handle non-object inputs correctly
b996f9f
Improve array test to ensure contains returns false for valid array keys
92779c2
Handle empty query string pairs correctly
c864665
Add invert.test.js and move tests from invert.js
c547fdc
Separate invert tests from implementation into invert.test.js
cdfdd7c
Co-authored-by: Isaac Abodunrin <bytesandroses@users.noreply.github.c…
7846797
deleted an unnecessary file
13eaa4f
Use Object.create(null) in tally to avoid prototype key conflicts
3cb7a22
Use Object.create(null) to avoid prototype key collisions in tally
cff46fb
deleted an empty space
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| function contains() {} | ||
| function contains(obj, key) { | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
|
|
||
| return Object.hasOwn(obj, key); | ||
| } | ||
|
|
||
| module.exports = contains; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,36 @@ | ||
| const contains = require("./contains.js"); | ||
|
|
||
| /* | ||
| Implement a function called contains that checks an object contains a | ||
| particular property | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'a') // returns true | ||
| as the object contains a key of 'a' | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'c') // returns false | ||
| as the object doesn't contains a key of 'c' | ||
| */ | ||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // Given a contains function | ||
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // When passed to contains with any key | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("returns false for an empty object", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("returns true for an existing property", () => { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| test("returns false for a non-existent property", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("returns false for an array", () => { | ||
| expect(contains([1, 2, 3], 0)).toBe(false); | ||
| }); | ||
|
|
||
| // Given invalid parameters like null | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("returns false for null", () => { | ||
| expect(contains(null, "a")).toBe(false); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,11 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| const result = {}; | ||
| for (let i = 0; i < pairs.length; i++) { | ||
| const [country, currency] = pairs[i]; | ||
|
|
||
| result[country] = currency; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,23 @@ | ||
| const createLookup = require("./lookup.js"); | ||
|
|
||
| test.todo("creates a country currency code lookup for multiple codes"); | ||
|
|
||
| /* | ||
|
|
||
| Create a lookup object of key value pairs from an array of code pairs | ||
|
|
||
| Acceptance Criteria: | ||
|
|
||
| Given | ||
| - An array of arrays representing country code and currency code pairs | ||
| e.g. [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
| - createLookup function is called with the country-currency array as an argument | ||
|
|
||
| Then | ||
| - It should return an object where: | ||
| - The keys are the country codes | ||
| - The values are the corresponding currency codes | ||
|
|
||
| Example | ||
| Given: [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
| createLookup(countryCurrencyPairs) is called | ||
|
|
||
| Then | ||
| It should return: | ||
| { | ||
| 'US': 'USD', | ||
| 'CA': 'CAD' | ||
| } | ||
| */ | ||
| test("creates a country currency code lookup for multiple codes", () => { | ||
| const input = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["GB", "GBP"], | ||
| ]; | ||
| const expected = { | ||
| US: "USD", | ||
| CA: "CAD", | ||
| GB: "GBP", | ||
| }; | ||
|
|
||
| expect(createLookup(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| test("creates a country currency code lookup for an empty array", () => { | ||
| const input = []; | ||
| const expected = {}; | ||
|
|
||
| expect(createLookup(input)).toEqual(expected); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,24 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
| const result = {}; | ||
| if (!queryString) return result; | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| } | ||
| const pairs = queryString.split("&"); | ||
|
|
||
| return queryParams; | ||
| pairs.forEach((pair) => { | ||
| if (pair === "") return; | ||
|
|
||
| const index = pair.indexOf("="); | ||
|
|
||
| if (index === -1) { | ||
| result[pair] = ""; | ||
| } else { | ||
| const key = pair.slice(0, index); | ||
| const value = pair.slice(index + 1); | ||
| result[key] = value; | ||
| } | ||
| }); | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| return result; | ||
| } | ||
|
|
||
| module.exports = parseQueryString; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| const result = Object.create(null); | ||
|
|
||
| items.forEach((item) => { | ||
| if (result[item]) { | ||
| result[item]++; | ||
| } else { | ||
| result[item] = 1; | ||
| } | ||
| }); | ||
|
|
||
| return result; | ||
| } | ||
| module.exports = tally; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| const invert = require("./invert"); | ||
|
|
||
| test("inverts a simple object", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("inverts an object with multiple key-value pairs", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ | ||
| 1: "a", | ||
| 2: "b", | ||
| }); | ||
| }); | ||
|
|
||
| test("returns empty object when given an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("handles non-string values as keys in the inverted object", () => { | ||
| expect(invert({ a: 1, b: true, c: null })).toEqual({ | ||
| 1: "a", | ||
| true: "b", | ||
| null: "c", | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.