@tmlmt/cooklang-parser 2.1.3 → 2.1.6

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/README.md CHANGED
@@ -60,9 +60,7 @@ console.log(recipe.timers); // [{ duration: 15, unit: "minutes", name: undefined
60
60
 
61
61
  ## Future plans
62
62
 
63
- I plan to further develop features depending on the needs or bugs I will encounter in using this library in a practical application. Current backlog is as follows:
64
-
65
- - Pantry parsing and basic functions (e.g. take pantry into account when creating a shopping list)
63
+ I plan to further develop features depending on the needs or bugs I will encounter in using this library in a practical application. The current backlog and status can be seen in the [Issues](https://github.com/tmlmt/cooklang-parser/issues) page.
66
64
 
67
65
  ## Test coverage
68
66
 
@@ -72,4 +70,4 @@ You can run the tests yourself by cloning the repository and running `pnpm test`
72
70
 
73
71
  ## Contributing
74
72
 
75
- If you find any issue with your own examples of recipes, feel free to open an Issue and if you want to help fix it, to submit a Pull Request.
73
+ If you find any issue with your own examples of recipes, feel free to open an Issue and if you want to help fix it, to submit a Pull Request (PR). Please follow the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec when submitting PRs.
package/dist/index.cjs CHANGED
@@ -563,10 +563,10 @@ function multiplyQuantityValue(value, factor) {
563
563
  if (value.type === "fixed") {
564
564
  const newValue = multiplyNumericValue(
565
565
  value.value,
566
- factor
566
+ (0, import_big.default)(factor)
567
567
  );
568
568
  if (factor === parseInt(factor.toString()) || // e.g. 2 === int
569
- 1 / factor === parseInt((1 / factor).toString())) {
569
+ (0, import_big.default)(1).div(factor).toNumber() === parseInt((0, import_big.default)(1).div(factor).toString())) {
570
570
  return {
571
571
  type: "fixed",
572
572
  value: newValue
@@ -907,6 +907,7 @@ function extractMetadata(content) {
907
907
  }
908
908
 
909
909
  // src/classes/recipe.ts
910
+ var import_big2 = __toESM(require("big.js"), 1);
910
911
  var Recipe = class _Recipe {
911
912
  /**
912
913
  * Creates a new Recipe instance.
@@ -1140,12 +1141,13 @@ var Recipe = class _Recipe {
1140
1141
  if (originalServings === void 0 || originalServings === 0) {
1141
1142
  throw new Error("Error scaling recipe: no initial servings value set");
1142
1143
  }
1143
- const factor = newServings / originalServings;
1144
+ const factor = (0, import_big2.default)(newServings).div(originalServings);
1144
1145
  return this.scaleBy(factor);
1145
1146
  }
1146
1147
  /**
1147
1148
  * Scales the recipe by a factor.
1148
- * @param factor - The factor to scale the recipe by.
1149
+ * @param factor - The factor to scale the recipe by. While integers can be passed as-is, it is recommended to pass fractions as
1150
+ * [Big](https://github.com/MikeMcl/big.js/) values, e.g. `Big(num).div(den)` in order to avoid undesirable floating point operation inaccuracies.
1149
1151
  * @returns A new Recipe instance with the scaled ingredients.
1150
1152
  */
1151
1153
  scaleBy(factor) {
@@ -1165,7 +1167,7 @@ var Recipe = class _Recipe {
1165
1167
  ...quantityPart,
1166
1168
  value: multiplyQuantityValue(
1167
1169
  quantityPart.value,
1168
- quantityPart.scalable ? factor : 1
1170
+ quantityPart.scalable ? (0, import_big2.default)(factor) : 1
1169
1171
  )
1170
1172
  };
1171
1173
  }
@@ -1184,13 +1186,15 @@ var Recipe = class _Recipe {
1184
1186
  }
1185
1187
  return ingredient;
1186
1188
  }).filter((ingredient) => ingredient.quantity !== null);
1187
- newRecipe.servings = originalServings * factor;
1189
+ newRecipe.servings = (0, import_big2.default)(originalServings).times(factor).toNumber();
1188
1190
  if (newRecipe.metadata.servings && this.metadata.servings) {
1189
1191
  if (floatRegex.test(String(this.metadata.servings).replace(",", ".").trim())) {
1190
1192
  const servingsValue = parseFloat(
1191
1193
  String(this.metadata.servings).replace(",", ".")
1192
1194
  );
1193
- newRecipe.metadata.servings = String(servingsValue * factor);
1195
+ newRecipe.metadata.servings = String(
1196
+ (0, import_big2.default)(servingsValue).times(factor).toNumber()
1197
+ );
1194
1198
  }
1195
1199
  }
1196
1200
  if (newRecipe.metadata.yield && this.metadata.yield) {
@@ -1198,7 +1202,9 @@ var Recipe = class _Recipe {
1198
1202
  const yieldValue = parseFloat(
1199
1203
  String(this.metadata.yield).replace(",", ".")
1200
1204
  );
1201
- newRecipe.metadata.yield = String(yieldValue * factor);
1205
+ newRecipe.metadata.yield = String(
1206
+ (0, import_big2.default)(yieldValue).times(factor).toNumber()
1207
+ );
1202
1208
  }
1203
1209
  }
1204
1210
  if (newRecipe.metadata.serves && this.metadata.serves) {
@@ -1206,7 +1212,9 @@ var Recipe = class _Recipe {
1206
1212
  const servesValue = parseFloat(
1207
1213
  String(this.metadata.serves).replace(",", ".")
1208
1214
  );
1209
- newRecipe.metadata.serves = String(servesValue * factor);
1215
+ newRecipe.metadata.serves = String(
1216
+ (0, import_big2.default)(servesValue).times(factor).toNumber()
1217
+ );
1210
1218
  }
1211
1219
  }
1212
1220
  return newRecipe;