Skip to content

Commit 53f7773

Browse files
authored
feat(ShoppingList): add a recipe with either a factor or a number of servings for scaling (#45)
* test(shopping_list): group tests for readability * feat(ShoppingList): add a recipe with either a factor or a number of servings for scaling Currently, it is only possible to scale with a scaling factor (e.g. x2) a recipe to add in the shopping list. This introduces the possibility to use a number of servings for scaling, instead Deprecation: the call signature `add_recipe(recipe: Recipe, factor?: number) is now deprecated and will be removed in v3
1 parent 4468cf9 commit 53f7773

File tree

4 files changed

+406
-268
lines changed

4 files changed

+406
-268
lines changed

src/classes/shopping_list.ts

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,14 @@ export class ShoppingList {
6464

6565
private calculate_ingredients() {
6666
this.ingredients = [];
67-
for (const { recipe, factor } of this.recipes) {
68-
const scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
67+
for (const addedRecipe of this.recipes) {
68+
let scaledRecipe: Recipe;
69+
if ("factor" in addedRecipe) {
70+
const { recipe, factor } = addedRecipe;
71+
scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
72+
} else {
73+
scaledRecipe = addedRecipe.recipe.scaleTo(addedRecipe.servings);
74+
}
6975

7076
for (const ingredient of scaledRecipe.ingredients) {
7177
// Do not add hidden ingredients to the shopping list
@@ -123,14 +129,37 @@ export class ShoppingList {
123129
}
124130
}
125131

132+
/**
133+
* Adds a recipe to the shopping list, then automatically
134+
* recalculates the quantities and recategorize the ingredients.
135+
* @param recipe - The recipe to add.
136+
* @param scaling - The scaling option for the recipe. Can be either a factor or a number of servings
137+
*/
138+
add_recipe(
139+
recipe: Recipe,
140+
scaling?: { factor: number } | { servings: number },
141+
): void;
126142
/**
127143
* Adds a recipe to the shopping list, then automatically
128144
* recalculates the quantities and recategorize the ingredients.
129145
* @param recipe - The recipe to add.
130146
* @param factor - The factor to scale the recipe by.
147+
* @deprecated since v2.0.3. Use the other call signature with `scaling` instead. Will be removed in v3
131148
*/
132-
add_recipe(recipe: Recipe, factor: number = 1) {
133-
this.recipes.push({ recipe, factor });
149+
add_recipe(recipe: Recipe, factor?: number): void;
150+
add_recipe(
151+
recipe: Recipe,
152+
scaling?: { factor: number } | { servings: number } | number,
153+
): void {
154+
if (typeof scaling === "number" || scaling === undefined) {
155+
this.recipes.push({ recipe, factor: scaling ?? 1 });
156+
} else {
157+
if ("factor" in scaling) {
158+
this.recipes.push({ recipe, factor: scaling.factor });
159+
} else {
160+
this.recipes.push({ recipe, servings: scaling.servings });
161+
}
162+
}
134163
this.calculate_ingredients();
135164
this.categorize();
136165
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import type {
2525
CookwareFlag,
2626
CategorizedIngredients,
2727
AddedRecipe,
28+
RecipeWithFactor,
29+
RecipeWithServings,
2830
CategoryIngredient,
2931
Category,
3032
QuantityPart,
@@ -55,6 +57,8 @@ export {
5557
CookwareFlag,
5658
CategorizedIngredients,
5759
AddedRecipe,
60+
RecipeWithFactor,
61+
RecipeWithServings,
5862
CategoryIngredient,
5963
Category,
6064
Section,

src/types.ts

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -361,16 +361,33 @@ export interface CategorizedIngredients {
361361
}
362362

363363
/**
364-
* Represents a recipe that has been added to a shopping list.
364+
* Represents a recipe together with a scaling factor
365365
* @category Types
366366
*/
367-
export interface AddedRecipe {
367+
export interface RecipeWithFactor {
368368
/** The recipe that was added. */
369369
recipe: Recipe;
370-
/** The factor the recipe was scaled by. */
370+
/** The factor the recipe is scaled by. */
371371
factor: number;
372372
}
373373

374+
/**
375+
* Represents a recipe together with a servings value for scaling
376+
* @category Types
377+
*/
378+
export interface RecipeWithServings {
379+
/** The recipe that was added. */
380+
recipe: Recipe;
381+
/** The servings the recipe is scaled to */
382+
servings: number;
383+
}
384+
385+
/**
386+
* Represents a recipe that has been added to a shopping list.
387+
* @category Types
388+
*/
389+
export type AddedRecipe = RecipeWithFactor | RecipeWithServings;
390+
374391
/**
375392
* Represents an ingredient that has been added to a shopping list
376393
* @category Types

0 commit comments

Comments
 (0)