@tmlmt/cooklang-parser 2.0.2 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -452,15 +452,30 @@ interface CategorizedIngredients {
452
452
  [category: string]: Ingredient[];
453
453
  }
454
454
  /**
455
- * Represents a recipe that has been added to a shopping list.
455
+ * Represents a recipe together with a scaling factor
456
456
  * @category Types
457
457
  */
458
- interface AddedRecipe {
458
+ interface RecipeWithFactor {
459
459
  /** The recipe that was added. */
460
460
  recipe: Recipe;
461
- /** The factor the recipe was scaled by. */
461
+ /** The factor the recipe is scaled by. */
462
462
  factor: number;
463
463
  }
464
+ /**
465
+ * Represents a recipe together with a servings value for scaling
466
+ * @category Types
467
+ */
468
+ interface RecipeWithServings {
469
+ /** The recipe that was added. */
470
+ recipe: Recipe;
471
+ /** The servings the recipe is scaled to */
472
+ servings: number;
473
+ }
474
+ /**
475
+ * Represents a recipe that has been added to a shopping list.
476
+ * @category Types
477
+ */
478
+ type AddedRecipe = RecipeWithFactor | RecipeWithServings;
464
479
  /**
465
480
  * Represents an ingredient in a category.
466
481
  * @category Types
@@ -580,11 +595,23 @@ declare class ShoppingList {
580
595
  */
581
596
  constructor(category_config_str?: string | CategoryConfig);
582
597
  private calculate_ingredients;
598
+ /**
599
+ * Adds a recipe to the shopping list, then automatically
600
+ * recalculates the quantities and recategorize the ingredients.
601
+ * @param recipe - The recipe to add.
602
+ * @param scaling - The scaling option for the recipe. Can be either a factor or a number of servings
603
+ */
604
+ add_recipe(recipe: Recipe, scaling?: {
605
+ factor: number;
606
+ } | {
607
+ servings: number;
608
+ }): void;
583
609
  /**
584
610
  * Adds a recipe to the shopping list, then automatically
585
611
  * recalculates the quantities and recategorize the ingredients.
586
612
  * @param recipe - The recipe to add.
587
613
  * @param factor - The factor to scale the recipe by.
614
+ * @deprecated since v2.0.3. Use the other call signature with `scaling` instead. Will be removed in v3
588
615
  */
589
616
  add_recipe(recipe: Recipe, factor?: number): void;
590
617
  /**
@@ -606,4 +633,4 @@ declare class ShoppingList {
606
633
  categorize(): void;
607
634
  }
608
635
 
609
- export { type AddedRecipe, type CategorizedIngredients, type Category, CategoryConfig, type CategoryIngredient, type Cookware, type CookwareFlag, type CookwareItem, type DecimalValue, type FixedValue, type FractionValue, type Ingredient, type IngredientExtras, type IngredientFlag, type IngredientItem, type Item, type Metadata, type Note, type QuantityPart, type Range, Recipe, Section, ShoppingList, type Step, type TextItem, type TextValue, type Timer, type TimerItem };
636
+ export { type AddedRecipe, type CategorizedIngredients, type Category, CategoryConfig, type CategoryIngredient, type Cookware, type CookwareFlag, type CookwareItem, type DecimalValue, type FixedValue, type FractionValue, type Ingredient, type IngredientExtras, type IngredientFlag, type IngredientItem, type Item, type Metadata, type Note, type QuantityPart, type Range, Recipe, type RecipeWithFactor, type RecipeWithServings, Section, ShoppingList, type Step, type TextItem, type TextValue, type Timer, type TimerItem };
package/dist/index.d.ts CHANGED
@@ -452,15 +452,30 @@ interface CategorizedIngredients {
452
452
  [category: string]: Ingredient[];
453
453
  }
454
454
  /**
455
- * Represents a recipe that has been added to a shopping list.
455
+ * Represents a recipe together with a scaling factor
456
456
  * @category Types
457
457
  */
458
- interface AddedRecipe {
458
+ interface RecipeWithFactor {
459
459
  /** The recipe that was added. */
460
460
  recipe: Recipe;
461
- /** The factor the recipe was scaled by. */
461
+ /** The factor the recipe is scaled by. */
462
462
  factor: number;
463
463
  }
464
+ /**
465
+ * Represents a recipe together with a servings value for scaling
466
+ * @category Types
467
+ */
468
+ interface RecipeWithServings {
469
+ /** The recipe that was added. */
470
+ recipe: Recipe;
471
+ /** The servings the recipe is scaled to */
472
+ servings: number;
473
+ }
474
+ /**
475
+ * Represents a recipe that has been added to a shopping list.
476
+ * @category Types
477
+ */
478
+ type AddedRecipe = RecipeWithFactor | RecipeWithServings;
464
479
  /**
465
480
  * Represents an ingredient in a category.
466
481
  * @category Types
@@ -580,11 +595,23 @@ declare class ShoppingList {
580
595
  */
581
596
  constructor(category_config_str?: string | CategoryConfig);
582
597
  private calculate_ingredients;
598
+ /**
599
+ * Adds a recipe to the shopping list, then automatically
600
+ * recalculates the quantities and recategorize the ingredients.
601
+ * @param recipe - The recipe to add.
602
+ * @param scaling - The scaling option for the recipe. Can be either a factor or a number of servings
603
+ */
604
+ add_recipe(recipe: Recipe, scaling?: {
605
+ factor: number;
606
+ } | {
607
+ servings: number;
608
+ }): void;
583
609
  /**
584
610
  * Adds a recipe to the shopping list, then automatically
585
611
  * recalculates the quantities and recategorize the ingredients.
586
612
  * @param recipe - The recipe to add.
587
613
  * @param factor - The factor to scale the recipe by.
614
+ * @deprecated since v2.0.3. Use the other call signature with `scaling` instead. Will be removed in v3
588
615
  */
589
616
  add_recipe(recipe: Recipe, factor?: number): void;
590
617
  /**
@@ -606,4 +633,4 @@ declare class ShoppingList {
606
633
  categorize(): void;
607
634
  }
608
635
 
609
- export { type AddedRecipe, type CategorizedIngredients, type Category, CategoryConfig, type CategoryIngredient, type Cookware, type CookwareFlag, type CookwareItem, type DecimalValue, type FixedValue, type FractionValue, type Ingredient, type IngredientExtras, type IngredientFlag, type IngredientItem, type Item, type Metadata, type Note, type QuantityPart, type Range, Recipe, Section, ShoppingList, type Step, type TextItem, type TextValue, type Timer, type TimerItem };
636
+ export { type AddedRecipe, type CategorizedIngredients, type Category, CategoryConfig, type CategoryIngredient, type Cookware, type CookwareFlag, type CookwareItem, type DecimalValue, type FixedValue, type FractionValue, type Ingredient, type IngredientExtras, type IngredientFlag, type IngredientItem, type Item, type Metadata, type Note, type QuantityPart, type Range, Recipe, type RecipeWithFactor, type RecipeWithServings, Section, ShoppingList, type Step, type TextItem, type TextValue, type Timer, type TimerItem };
package/dist/index.js CHANGED
@@ -1216,8 +1216,14 @@ var ShoppingList = class {
1216
1216
  }
1217
1217
  calculate_ingredients() {
1218
1218
  this.ingredients = [];
1219
- for (const { recipe, factor } of this.recipes) {
1220
- const scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
1219
+ for (const addedRecipe of this.recipes) {
1220
+ let scaledRecipe;
1221
+ if ("factor" in addedRecipe) {
1222
+ const { recipe, factor } = addedRecipe;
1223
+ scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
1224
+ } else {
1225
+ scaledRecipe = addedRecipe.recipe.scaleTo(addedRecipe.servings);
1226
+ }
1221
1227
  for (const ingredient of scaledRecipe.ingredients) {
1222
1228
  if (ingredient.flags && ingredient.flags.includes("hidden")) {
1223
1229
  continue;
@@ -1266,14 +1272,16 @@ var ShoppingList = class {
1266
1272
  }
1267
1273
  }
1268
1274
  }
1269
- /**
1270
- * Adds a recipe to the shopping list, then automatically
1271
- * recalculates the quantities and recategorize the ingredients.
1272
- * @param recipe - The recipe to add.
1273
- * @param factor - The factor to scale the recipe by.
1274
- */
1275
- add_recipe(recipe, factor = 1) {
1276
- this.recipes.push({ recipe, factor });
1275
+ add_recipe(recipe, scaling) {
1276
+ if (typeof scaling === "number" || scaling === void 0) {
1277
+ this.recipes.push({ recipe, factor: scaling ?? 1 });
1278
+ } else {
1279
+ if ("factor" in scaling) {
1280
+ this.recipes.push({ recipe, factor: scaling.factor });
1281
+ } else {
1282
+ this.recipes.push({ recipe, servings: scaling.servings });
1283
+ }
1284
+ }
1277
1285
  this.calculate_ingredients();
1278
1286
  this.categorize();
1279
1287
  }