@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.cjs CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
7
9
  var __export = (target, all) => {
@@ -16,6 +18,14 @@ var __copyProps = (to, from, except, desc) => {
16
18
  }
17
19
  return to;
18
20
  };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
19
29
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
20
30
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
21
31
 
@@ -342,6 +352,7 @@ var numberLikeRegex = d().startAnchor().digit().oneOrMore().startGroup().anyOf("
342
352
  var floatRegex = d().startAnchor().digit().oneOrMore().startGroup().anyOf(".").exactly(1).digit().oneOrMore().endGroup().optional().endAnchor().toRegExp();
343
353
 
344
354
  // src/units.ts
355
+ var import_big = __toESM(require("big.js"), 1);
345
356
  var units = [
346
357
  // Mass (Metric)
347
358
  {
@@ -493,9 +504,9 @@ function simplifyFraction(num, den) {
493
504
  }
494
505
  function multiplyNumericValue(v, factor) {
495
506
  if (v.type === "decimal") {
496
- return { type: "decimal", value: v.value * factor };
507
+ return { type: "decimal", value: (0, import_big.default)(v.value).times(factor).toNumber() };
497
508
  }
498
- return simplifyFraction(v.num * factor, v.den);
509
+ return simplifyFraction((0, import_big.default)(v.num).times(factor).toNumber(), v.den);
499
510
  }
500
511
  function addNumericValues(val1, val2) {
501
512
  let num1;
@@ -524,7 +535,10 @@ function addNumericValues(val1, val2) {
524
535
  const sumNum = num1 * den2 + num2 * den1;
525
536
  return simplifyFraction(sumNum, commonDen);
526
537
  } else {
527
- return { type: "decimal", value: num1 / den1 + num2 / den2 };
538
+ return {
539
+ type: "decimal",
540
+ value: (0, import_big.default)(num1).div(den1).add((0, import_big.default)(num2).div(den2)).toNumber()
541
+ };
528
542
  }
529
543
  }
530
544
  var toRoundedDecimal = (v) => {
@@ -551,8 +565,8 @@ function multiplyQuantityValue(value, factor) {
551
565
  }
552
566
  return {
553
567
  type: "range",
554
- min: toRoundedDecimal(multiplyNumericValue(value.min, factor)),
555
- max: toRoundedDecimal(multiplyNumericValue(value.max, factor))
568
+ min: multiplyNumericValue(value.min, factor),
569
+ max: multiplyNumericValue(value.max, factor)
556
570
  };
557
571
  }
558
572
  var convertQuantityValue = (value, def, targetDef) => {
@@ -1243,8 +1257,14 @@ var ShoppingList = class {
1243
1257
  }
1244
1258
  calculate_ingredients() {
1245
1259
  this.ingredients = [];
1246
- for (const { recipe, factor } of this.recipes) {
1247
- const scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
1260
+ for (const addedRecipe of this.recipes) {
1261
+ let scaledRecipe;
1262
+ if ("factor" in addedRecipe) {
1263
+ const { recipe, factor } = addedRecipe;
1264
+ scaledRecipe = factor === 1 ? recipe : recipe.scaleBy(factor);
1265
+ } else {
1266
+ scaledRecipe = addedRecipe.recipe.scaleTo(addedRecipe.servings);
1267
+ }
1248
1268
  for (const ingredient of scaledRecipe.ingredients) {
1249
1269
  if (ingredient.flags && ingredient.flags.includes("hidden")) {
1250
1270
  continue;
@@ -1293,14 +1313,16 @@ var ShoppingList = class {
1293
1313
  }
1294
1314
  }
1295
1315
  }
1296
- /**
1297
- * Adds a recipe to the shopping list, then automatically
1298
- * recalculates the quantities and recategorize the ingredients.
1299
- * @param recipe - The recipe to add.
1300
- * @param factor - The factor to scale the recipe by.
1301
- */
1302
- add_recipe(recipe, factor = 1) {
1303
- this.recipes.push({ recipe, factor });
1316
+ add_recipe(recipe, scaling) {
1317
+ if (typeof scaling === "number" || scaling === void 0) {
1318
+ this.recipes.push({ recipe, factor: scaling ?? 1 });
1319
+ } else {
1320
+ if ("factor" in scaling) {
1321
+ this.recipes.push({ recipe, factor: scaling.factor });
1322
+ } else {
1323
+ this.recipes.push({ recipe, servings: scaling.servings });
1324
+ }
1325
+ }
1304
1326
  this.calculate_ingredients();
1305
1327
  this.categorize();
1306
1328
  }