-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
combine recipes before analysing menu
- Loading branch information
1 parent
e118f76
commit 43fe80c
Showing
5 changed files
with
64 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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 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
35 changes: 35 additions & 0 deletions
35
src/app/analysis/components/form/utils/combile-recipes.test.ts
This file contains 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,35 @@ | ||
import { combineRecipes } from "./combine-recipes"; | ||
import recipe from '@/app/test_objects/loaded-recipe.json'; | ||
|
||
describe('combine recipes', () => { | ||
|
||
it('should remove recipes with zero servings and combine recipes with the same id', () => { | ||
|
||
const recipes = [ | ||
{ | ||
selectedRecipeId: recipe.id, | ||
selectedRecipe: recipe.recipe, | ||
selectedServings: 0 | ||
}, | ||
{ | ||
selectedRecipeId: recipe.id, | ||
selectedRecipe: recipe.recipe, | ||
selectedServings: 3 | ||
}, | ||
{ | ||
selectedRecipeId: recipe.id, | ||
selectedRecipe: recipe.recipe, | ||
selectedServings: 2 | ||
}, | ||
]; | ||
|
||
const combinedRecipes = [ | ||
{ | ||
selectedRecipeId: recipe.id, | ||
selectedRecipe: recipe.recipe, | ||
selectedServings: 5 | ||
} | ||
] | ||
expect(combineRecipes(recipes)).toEqual(combinedRecipes); | ||
}) | ||
}) |
This file contains 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,21 @@ | ||
import { RecipeWithServings } from "@/app/types/types"; | ||
|
||
export const combineRecipes = (currentRecipes: RecipeWithServings[]): RecipeWithServings[] => { | ||
|
||
const recipes = currentRecipes.filter(recipe => recipe.selectedServings != 0); | ||
const combinedRecipes = []; | ||
|
||
for (const recipe of recipes) { | ||
const { selectedRecipeId: id, selectedServings, selectedRecipe } = recipe; | ||
|
||
const existingRecipeIndex = combinedRecipes.findIndex(r => r.selectedRecipeId === id); | ||
|
||
if (existingRecipeIndex !== -1) { | ||
combinedRecipes[existingRecipeIndex].selectedServings += selectedServings; | ||
} else { | ||
combinedRecipes.push({ selectedRecipeId: id, selectedServings, selectedRecipe }); | ||
} | ||
} | ||
|
||
return combinedRecipes; | ||
} |
This file contains 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