@tmlmt/cooklang-parser 2.1.3 → 2.1.4

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
@@ -1,3 +1,5 @@
1
+ import Big from 'big.js';
2
+
1
3
  /**
2
4
  * Represents a recipe section
3
5
  *
@@ -108,10 +110,11 @@ declare class Recipe {
108
110
  scaleTo(newServings: number): Recipe;
109
111
  /**
110
112
  * Scales the recipe by a factor.
111
- * @param factor - The factor to scale the recipe by.
113
+ * @param factor - The factor to scale the recipe by. While integers can be passed as-is, it is recommended to pass fractions as
114
+ * [Big](https://github.com/MikeMcl/big.js/) values, e.g. `Big(num).div(den)` in order to avoid undesirable floating point operation inaccuracies.
112
115
  * @returns A new Recipe instance with the scaled ingredients.
113
116
  */
114
- scaleBy(factor: number): Recipe;
117
+ scaleBy(factor: number | Big): Recipe;
115
118
  /**
116
119
  * Gets the number of servings for the recipe.
117
120
  * @private
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import Big from 'big.js';
2
+
1
3
  /**
2
4
  * Represents a recipe section
3
5
  *
@@ -108,10 +110,11 @@ declare class Recipe {
108
110
  scaleTo(newServings: number): Recipe;
109
111
  /**
110
112
  * Scales the recipe by a factor.
111
- * @param factor - The factor to scale the recipe by.
113
+ * @param factor - The factor to scale the recipe by. While integers can be passed as-is, it is recommended to pass fractions as
114
+ * [Big](https://github.com/MikeMcl/big.js/) values, e.g. `Big(num).div(den)` in order to avoid undesirable floating point operation inaccuracies.
112
115
  * @returns A new Recipe instance with the scaled ingredients.
113
116
  */
114
- scaleBy(factor: number): Recipe;
117
+ scaleBy(factor: number | Big): Recipe;
115
118
  /**
116
119
  * Gets the number of servings for the recipe.
117
120
  * @private
package/dist/index.js CHANGED
@@ -526,10 +526,10 @@ function multiplyQuantityValue(value, factor) {
526
526
  if (value.type === "fixed") {
527
527
  const newValue = multiplyNumericValue(
528
528
  value.value,
529
- factor
529
+ Big(factor)
530
530
  );
531
531
  if (factor === parseInt(factor.toString()) || // e.g. 2 === int
532
- 1 / factor === parseInt((1 / factor).toString())) {
532
+ Big(1).div(factor).toNumber() === parseInt(Big(1).div(factor).toString())) {
533
533
  return {
534
534
  type: "fixed",
535
535
  value: newValue
@@ -870,6 +870,7 @@ function extractMetadata(content) {
870
870
  }
871
871
 
872
872
  // src/classes/recipe.ts
873
+ import Big2 from "big.js";
873
874
  var Recipe = class _Recipe {
874
875
  /**
875
876
  * Creates a new Recipe instance.
@@ -1103,12 +1104,13 @@ var Recipe = class _Recipe {
1103
1104
  if (originalServings === void 0 || originalServings === 0) {
1104
1105
  throw new Error("Error scaling recipe: no initial servings value set");
1105
1106
  }
1106
- const factor = newServings / originalServings;
1107
+ const factor = Big2(newServings).div(originalServings);
1107
1108
  return this.scaleBy(factor);
1108
1109
  }
1109
1110
  /**
1110
1111
  * Scales the recipe by a factor.
1111
- * @param factor - The factor to scale the recipe by.
1112
+ * @param factor - The factor to scale the recipe by. While integers can be passed as-is, it is recommended to pass fractions as
1113
+ * [Big](https://github.com/MikeMcl/big.js/) values, e.g. `Big(num).div(den)` in order to avoid undesirable floating point operation inaccuracies.
1112
1114
  * @returns A new Recipe instance with the scaled ingredients.
1113
1115
  */
1114
1116
  scaleBy(factor) {
@@ -1128,7 +1130,7 @@ var Recipe = class _Recipe {
1128
1130
  ...quantityPart,
1129
1131
  value: multiplyQuantityValue(
1130
1132
  quantityPart.value,
1131
- quantityPart.scalable ? factor : 1
1133
+ quantityPart.scalable ? Big2(factor) : 1
1132
1134
  )
1133
1135
  };
1134
1136
  }
@@ -1147,13 +1149,15 @@ var Recipe = class _Recipe {
1147
1149
  }
1148
1150
  return ingredient;
1149
1151
  }).filter((ingredient) => ingredient.quantity !== null);
1150
- newRecipe.servings = originalServings * factor;
1152
+ newRecipe.servings = Big2(originalServings).times(factor).toNumber();
1151
1153
  if (newRecipe.metadata.servings && this.metadata.servings) {
1152
1154
  if (floatRegex.test(String(this.metadata.servings).replace(",", ".").trim())) {
1153
1155
  const servingsValue = parseFloat(
1154
1156
  String(this.metadata.servings).replace(",", ".")
1155
1157
  );
1156
- newRecipe.metadata.servings = String(servingsValue * factor);
1158
+ newRecipe.metadata.servings = String(
1159
+ Big2(servingsValue).times(factor).toNumber()
1160
+ );
1157
1161
  }
1158
1162
  }
1159
1163
  if (newRecipe.metadata.yield && this.metadata.yield) {
@@ -1161,7 +1165,9 @@ var Recipe = class _Recipe {
1161
1165
  const yieldValue = parseFloat(
1162
1166
  String(this.metadata.yield).replace(",", ".")
1163
1167
  );
1164
- newRecipe.metadata.yield = String(yieldValue * factor);
1168
+ newRecipe.metadata.yield = String(
1169
+ Big2(yieldValue).times(factor).toNumber()
1170
+ );
1165
1171
  }
1166
1172
  }
1167
1173
  if (newRecipe.metadata.serves && this.metadata.serves) {
@@ -1169,7 +1175,9 @@ var Recipe = class _Recipe {
1169
1175
  const servesValue = parseFloat(
1170
1176
  String(this.metadata.serves).replace(",", ".")
1171
1177
  );
1172
- newRecipe.metadata.serves = String(servesValue * factor);
1178
+ newRecipe.metadata.serves = String(
1179
+ Big2(servesValue).times(factor).toNumber()
1180
+ );
1173
1181
  }
1174
1182
  }
1175
1183
  return newRecipe;