-
Notifications
You must be signed in to change notification settings - Fork 11
Added and completed all of the test functions for data-and-functions … #3
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: master
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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const getActiveUsers = (data) => { | ||
| if (data == null || data.users == null) { | ||
| return null | ||
| } else { | ||
| const activeUsers = [] | ||
| data.users.forEach((u) => { | ||
| if (u.accountActive === true) { | ||
|
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. You've already got a boolean. No need to create another one: |
||
| activeUsers.push(u) | ||
| } | ||
| }) | ||
| return activeUsers | ||
| } | ||
| } | ||
|
|
||
| export default getActiveUsers | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| const getMostExpensiveProduct = (data) => { | ||
| if (data == null || data.products == null) { | ||
| return null | ||
| } else { | ||
| let mostExpensive = data.products[0] | ||
| data.products.forEach((p) => { | ||
| if (mostExpensive.price < p.price) { | ||
| mostExpensive = p | ||
| } | ||
| }) | ||
| return mostExpensive | ||
| } | ||
| } | ||
|
|
||
| export default getMostExpensiveProduct |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import getProductById from './getProductById' | ||
| import getUserById from './getUserById' | ||
|
|
||
| const getOrderInfo = (data) => { | ||
| if (data == null || data.orders == null) { | ||
| return null | ||
| } else { | ||
| const orders = [] | ||
| data.orders.forEach((o) => { | ||
|
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. Try writing this as a |
||
| orders.push({ | ||
| orderId: o.id, | ||
| price: getProductById(data, o.productId).price, | ||
| userName: getUserById(data, o.userId).name | ||
| }) | ||
| }) | ||
| return orders | ||
| } | ||
| } | ||
|
|
||
| export default getOrderInfo | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const getProductById = (data, id) => { | ||
| if (data == null || data.products == null || id == null) { | ||
| return null | ||
| } else { | ||
| return data.products.find((p) => { | ||
|
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. Nice use of |
||
| return p.id === id | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export default getProductById | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| const getUserById = (data, id) => { | ||
| if (data == null || id == null || data.users == null) { | ||
| return null | ||
| } else { | ||
| return data.users.find((u) => u.id === id) | ||
| } | ||
| } | ||
|
|
||
| export default getUserById |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| const getProductById = (data, id) => { | ||
| if (data == null || data.products == null || id == null) { | ||
| return null | ||
| } else { | ||
| return data.products.find((p) => { | ||
| return p.id === id | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| export default getProductById |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import getProductById from './getProductById' | ||
|
|
||
| const getProductsForOrder = (data, id) => { | ||
| if (data == null || id == null || data.orders == null) { | ||
| throw new Error('Missing data or ID') | ||
| } | ||
| const order = data.orders.find((o) => o.id === id) | ||
| if (order === undefined) { | ||
|
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. You can totally do the test the way you have it, but you could also just shorten it to: |
||
| return null | ||
| } | ||
| const productArray = [] | ||
| order.products.forEach((p) => { | ||
|
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. You could use |
||
| productArray.push(getProductById(data, p)) | ||
| }) | ||
| return productArray | ||
| } | ||
|
|
||
| export default getProductsForOrder | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import getProductById from './getProductById' | ||
|
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. Try importing |
||
|
|
||
| const getTotalPriceForOrder = (data, id) => { | ||
| if (data == null || id == null || data.orders == null) { | ||
| throw new Error('Missing data, id, or orders.') | ||
| } | ||
| const order = data.orders.find((o) => o.id === id) | ||
| if (order === undefined) { | ||
| return null | ||
| } | ||
| let totalPrice = 0 | ||
| order.products.forEach((p) => { | ||
| totalPrice += getProductById(data, p).price | ||
| }) | ||
| return totalPrice | ||
| } | ||
|
|
||
| export default getTotalPriceForOrder | ||
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.
You can dump the
elseblock if you want.