-
-
Notifications
You must be signed in to change notification settings - Fork 271
Manchester | 26-ITP-Jan | Mehroz Munir | Sprint 2 | Coursework #1051
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| function contains() {} | ||
|
|
||
| function contains(object, property) { | ||
| if (object === null || typeof object !== "object" || Array.isArray(object)) | ||
| throw new Error("Input is not a valid object"); | ||
| else return Object.hasOwn(object, property); | ||
| } | ||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,29 +7,84 @@ 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 | ||
| describe("containsProperty", () => { | ||
| // 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 | ||
| [ | ||
| { input: { a: 1, b: 2 }, property: "a", expected: true }, | ||
| { input: { a: 1, b: 2 }, property: "c", expected: false }, | ||
| { input: { a: 1, b: 2, sdk: 28299 }, property: "what", expected: false }, | ||
| ].forEach(({ input, property, expected }) => | ||
| it(`Should return true if the object ${input} contains the property ${property} otherwise false`, () => | ||
| expect(contains(input, property)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| [ | ||
| { input: {}, property: "a", expected: false }, | ||
| { input: {}, property: "", expected: false }, | ||
| { input: {}, property: null, expected: false }, | ||
| ].forEach(({ input, property, expected }) => | ||
| it(`Should return false if the object ${input} is empty`, () => | ||
| expect(contains(input, property)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| [ | ||
| { input: { a: 1, dog: 2 }, property: "dog", expected: true }, | ||
| { | ||
| input: { a: 1, b: 2, property3: "here you go" }, | ||
| property: "property3", | ||
| expected: true, | ||
| }, | ||
| { input: { a: 1, b: 2, sdk: 28299 }, property: "sdk", expected: true }, | ||
| ].forEach(({ input, property, expected }) => | ||
| it(`Should return true if the object ${input} contains the property ${property}`, () => | ||
| expect(contains(input, property)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
| [ | ||
| { input: { a: 1, dog: 2 }, property: "cat", expected: false }, | ||
| { | ||
| input: { a: 1, b: 2, property3: "here you go" }, | ||
| property: "", | ||
| expected: false, | ||
| }, | ||
| { | ||
| input: { a: 1, b: 2, thatProperty: 28299 }, | ||
| property: "", | ||
| expected: false, | ||
| }, | ||
| ].forEach(({ input, property, expected }) => | ||
| it(`Should return false if the object ${input} doesn't contain the property ${property}`, () => | ||
| expect(contains(input, property)).toEqual(expected)) | ||
| ); | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| [ | ||
| { input: [1], property: "a" }, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test does not yet confirm that the function correctly returns false when the first argument is an array. function contains(obj, key) {
if (obj == null || typeof obj != "object") return false;
return Object.hasOwn(obj, key);
}This happens because arrays are objects, with their indices acting as keys. For example, To properly verify this behavior, the test should use a non-empty array along with a valid key to |
||
| { input: null, property: "c" }, | ||
| { input: undefined, property: "what" }, | ||
| { input: [1, 2, 3], property: "what" }, | ||
| ].forEach(({ input, property }) => | ||
| it(`Should return false as ${input} is not an object`, () => | ||
| expect(() => contains(input, property)).toThrow( | ||
| "Input is not a valid object" | ||
| )) | ||
| ); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| function createLookup() { | ||
| function createLookup(array) { | ||
| // implementation here | ||
| if (!Array.isArray(array) || !array.every((item) => Array.isArray(item))) | ||
| throw new Error("Invalid Input"); | ||
| return Object.fromEntries(array); | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| 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 | ||
|
|
@@ -33,3 +31,41 @@ It should return: | |
| 'CA': 'CAD' | ||
| } | ||
| */ | ||
|
|
||
| describe("lookup", () => { | ||
| const countryCurrencyArray = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["PAK", "RS"], | ||
| ]; | ||
| const countryCurrencyObject = { | ||
| US: "USD", | ||
| CA: "CAD", | ||
| PAK: "RS", | ||
| }; | ||
|
|
||
| // CASE 1: Should return an object with key as country codes and value | ||
| // as currency codes when an array is passed containing arrays of countries | ||
| // codes and their currencies codes | ||
| test(`should return an object of the array [${countryCurrencyArray}]`, () => | ||
| expect(createLookup(countryCurrencyArray)).toEqual(countryCurrencyObject)); | ||
|
|
||
| // CASE 2: Should return an empty object if empty array is passed | ||
| test(`should return an empty object if the array is empty`, () => | ||
| expect(createLookup([[]])).toEqual({})); | ||
|
Comment on lines
+53
to
+55
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The first argument is not quite an empty array; it is "an array containing an empty array". For this input, your function actually returns |
||
|
|
||
| // CASE 3: Input is null | ||
| test(`should throw an error if null is passed`, () => | ||
| expect(() => createLookup(null)).toThrow("Invalid Input")); | ||
| // CASE 4: Input is undefined | ||
| test(`should throw an error if undefined is passed`, () => | ||
| expect(() => createLookup(undefined)).toThrow("Invalid Input")); | ||
|
|
||
| // CASE 5: Input is empty object {} | ||
| test(`should throw an error if undefined is passed`, () => | ||
| expect(() => createLookup({})).toThrow("Invalid Input")); | ||
|
|
||
| // CASE 5: Input is single value {} | ||
| test(`should throw an error if single value array is passed`, () => | ||
| expect(() => createLookup([123])).toThrow("Invalid Input")); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,13 @@ | ||
| function parseQueryString(queryString) { | ||
| if (typeof queryString !== "string") throw new Error("Invalid Input"); | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| const [key, value] = pair.split(/=(.*)/); | ||
| queryParams[key] = value; | ||
| } | ||
|
Comment on lines
9
to
12
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (1) For the following function call, does your function return the value you expect? (2) Can you explain how |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,10 @@ | ||
| function tally() {} | ||
|
|
||
| function tally(array) { | ||
| if (!Array.isArray(array)) throw new Error("Not an array"); | ||
| const tallyObject = {}; | ||
| for (const item of array) { | ||
| if (Object.hasOwn(tallyObject, item)) tallyObject[item] += 1; | ||
| else tallyObject[item] = 1; | ||
| } | ||
| return tallyObject; | ||
| } | ||
| module.exports = tally; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you use an approach that can work for any number of ingredients?
Suggestion: A for-of loop or
Array.prototype.join().