Repository navigation
London | 26-ITP-Sep | Diana Ausiejute | Sprint 2 | Coursework #1546
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
65dcedf
cefdbcc
d9c82d9
d627dd1
f6ccd35
ef14433
0b31435
65b2a62
79d9c90
e5fd181
4297e40
d701c04
47eb147
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 |
|---|---|---|
|
|
@@ -4,3 +4,5 @@ node_modules | |
| **/.DS_Store | ||
| .idea | ||
| package-lock.json | ||
| mean.test.js | ||
| mean.js | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,9 @@ | ||
| function contains() {} | ||
| function contains(obj, key) { | ||
| if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { | ||
| throw new Error("contains requires an object"); | ||
| } | ||
|
|
||
| return Object.hasOwn(obj, key); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| } | ||
| function createLookup(countryCurrencyPairs) { | ||
| const lookup = {}; | ||
|
|
||
| countryCurrencyPairs.forEach(function (pair) { | ||
| const country = pair[0]; | ||
| const currency = pair[1]; | ||
| lookup[country] = currency; | ||
| }); | ||
|
|
||
| return lookup; | ||
| } | ||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,29 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
|
|
||
| if (queryString.startsWith("?")) { | ||
| queryString = queryString.slice(1); | ||
| } | ||
|
|
||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| if (pair === "") { | ||
| continue; | ||
| } | ||
|
|
||
| const parts = pair.split("="); | ||
| const key = parts[0]; | ||
| const value = parts.slice(1).join("="); | ||
|
|
||
| const decodeKey = decodeURIComponent(key.replace(/\+/g, " ")); | ||
| const decodeValue = decodeURIComponent(value.replace(/\+/g, " ")); | ||
|
Comment on lines
+23
to
+24
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 regex works fine here. Can you think of something else that would be more readable for a reader who doesn’t want to decode the
Author
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. could also use the replaceAll. like so: 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. That’s correct, well done! |
||
|
|
||
| queryParams[decodeKey] = decodeValue; | ||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| 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("tally requires an array"); | ||
| } | ||
|
|
||
| const counts = {}; | ||
|
|
||
| for (const item of items) { | ||
| if (Object.hasOwn(counts, item)) { | ||
| counts[item] += 1; | ||
| } else { | ||
| counts[item] = 1; | ||
| } | ||
| } | ||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("invert should swap each key and value when passed an object", () => { | ||
| expect(invert({ a: "cherry", b: "kiwi" })).toEqual({ | ||
| cherry: "a", | ||
| kiwi: "b", | ||
| }); | ||
| }); | ||
|
|
||
| test("invert should return an empty object when passed an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); |
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.
The string, number, null, and missing-argument cases place
.toThrow(...)inside the callback, so Jest never receives atoThrowassertion. The null case also passes a string as the object andnullas the key.To "like" or "dislike" this comment, please follow this link