@tmlmt/cooklang-parser 2.0.2 → 2.1.1

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.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
@@ -315,6 +315,7 @@ var numberLikeRegex = d().startAnchor().digit().oneOrMore().startGroup().anyOf("
315
315
  var floatRegex = d().startAnchor().digit().oneOrMore().startGroup().anyOf(".").exactly(1).digit().oneOrMore().endGroup().optional().endAnchor().toRegExp();
316
316
 
317
317
  // src/units.ts
318
+ import Big from "big.js";
318
319
  var units = [
319
320
  // Mass (Metric)
320
321
  {
@@ -466,9 +467,9 @@ function simplifyFraction(num, den) {
466
467
  }
467
468
  function multiplyNumericValue(v, factor) {
468
469
  if (v.type === "decimal") {
469
- return { type: "decimal", value: v.value * factor };
470
+ return { type: "decimal", value: Big(v.value).times(factor).toNumber() };
470
471
  }
471
- return simplifyFraction(v.num * factor, v.den);
472
+ return simplifyFraction(Big(v.num).times(factor).toNumber(), v.den);
472
473
  }
473
474
  function addNumericValues(val1, val2) {
474
475
  let num1;
@@ -497,7 +498,10 @@ function addNumericValues(val1, val2) {
497
498
  const sumNum = num1 * den2 + num2 * den1;
498
499
  return simplifyFraction(sumNum, commonDen);
499
500
  } else {
500
- return { type: "decimal", value: num1 / den1 + num2 / den2 };
501
+ return {
502
+ type: "decimal",
503
+ value: Big(num1).div(den1).add(Big(num2).div(den2)).toNumber()
504
+ };
501
505
  }
502
506
  }
503
507
  var toRoundedDecimal = (v) => {
@@ -524,8 +528,8 @@ function multiplyQuantityValue(value, factor) {
524
528
  }
525
529
  return {
526
530
  type: "range",
527
- min: toRoundedDecimal(multiplyNumericValue(value.min, factor)),
528
- max: toRoundedDecimal(multiplyNumericValue(value.max, factor))
531
+ min: multiplyNumericValue(value.min, factor),
532
+ max: multiplyNumericValue(value.max, factor)
529
533
  };
530
534
  }
531
535
  var convertQuantityValue = (value, def, targetDef) => {
@@ -1216,8 +1220,14 @@ var ShoppingList = class {
1216
1220
  }
1217
1221
  calculate_ingredients() {
1218
1222
  this.ingredients = [];
1219
- for (const { recipe, factor } of this.recipes) {
1220
- const scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
1223
+ for (const addedRecipe of this.recipes) {
1224
+ let scaledRecipe;
1225
+ if ("factor" in addedRecipe) {
1226
+ const { recipe, factor } = addedRecipe;
1227
+ scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
1228
+ } else {
1229
+ scaledRecipe = addedRecipe.recipe.scaleTo(addedRecipe.servings);
1230
+ }
1221
1231
  for (const ingredient of scaledRecipe.ingredients) {
1222
1232
  if (ingredient.flags && ingredient.flags.includes("hidden")) {
1223
1233
  continue;
@@ -1266,14 +1276,16 @@ var ShoppingList = class {
1266
1276
  }
1267
1277
  }
1268
1278
  }
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 });
1279
+ add_recipe(recipe, scaling) {
1280
+ if (typeof scaling === "number" || scaling === void 0) {
1281
+ this.recipes.push({ recipe, factor: scaling ?? 1 });
1282
+ } else {
1283
+ if ("factor" in scaling) {
1284
+ this.recipes.push({ recipe, factor: scaling.factor });
1285
+ } else {
1286
+ this.recipes.push({ recipe, servings: scaling.servings });
1287
+ }
1288
+ }
1277
1289
  this.calculate_ingredients();
1278
1290
  this.categorize();
1279
1291
  }