@tmlmt/cooklang-parser 1.2.5 → 1.4.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.js CHANGED
@@ -2,16 +2,15 @@ var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
4
4
 
5
- // src/classes/aisle_config.ts
6
- var AisleConfig = class {
5
+ // src/classes/category_config.ts
6
+ var CategoryConfig = class {
7
7
  /**
8
- * Creates a new AisleConfig instance.
9
- * @param config - The aisle configuration to parse.
8
+ * Creates a new CategoryConfig instance.
9
+ * @param config - The category configuration to parse.
10
10
  */
11
11
  constructor(config) {
12
12
  /**
13
- * The categories of aisles.
14
- * @see {@link AisleCategory}
13
+ * The parsed categories of ingredients.
15
14
  */
16
15
  __publicField(this, "categories", []);
17
16
  if (config) {
@@ -19,8 +18,9 @@ var AisleConfig = class {
19
18
  }
20
19
  }
21
20
  /**
22
- * Parses an aisle configuration from a string.
23
- * @param config - The aisle configuration to parse.
21
+ * Parses a category configuration from a string into property
22
+ * {@link CategoryConfig.categories | categories}
23
+ * @param config - The category configuration to parse.
24
24
  */
25
25
  parse(config) {
26
26
  let currentCategory = null;
@@ -70,7 +70,10 @@ var Section = class {
70
70
  * @param name - The name of the section. Defaults to an empty string.
71
71
  */
72
72
  constructor(name = "") {
73
- /** The name of the section. Can be an empty string for the default (first) section. */
73
+ /**
74
+ * The name of the section. Can be an empty string for the default (first) section.
75
+ * @defaultValue `""`
76
+ */
74
77
  __publicField(this, "name");
75
78
  /** An array of steps and notes that make up the content of the section. */
76
79
  __publicField(this, "content", []);
@@ -316,14 +319,14 @@ var units = [
316
319
  name: "g",
317
320
  type: "mass",
318
321
  system: "metric",
319
- aliases: ["gram", "grams"],
322
+ aliases: ["gram", "grams", "grammes"],
320
323
  toBase: 1
321
324
  },
322
325
  {
323
326
  name: "kg",
324
327
  type: "mass",
325
328
  system: "metric",
326
- aliases: ["kilogram", "kilograms"],
329
+ aliases: ["kilogram", "kilograms", "kilogrammes", "kilos", "kilo"],
327
330
  toBase: 1e3
328
331
  },
329
332
  // Mass (Imperial)
@@ -346,7 +349,7 @@ var units = [
346
349
  name: "ml",
347
350
  type: "volume",
348
351
  system: "metric",
349
- aliases: ["milliliter", "milliliters", "millilitre", "millilitres"],
352
+ aliases: ["milliliter", "milliliters", "millilitre", "millilitres", "cc"],
350
353
  toBase: 1
351
354
  },
352
355
  {
@@ -411,7 +414,7 @@ var units = [
411
414
  name: "piece",
412
415
  type: "count",
413
416
  system: "metric",
414
- aliases: ["pieces"],
417
+ aliases: ["pieces", "pc"],
415
418
  toBase: 1
416
419
  }
417
420
  ];
@@ -766,32 +769,31 @@ var Recipe = class _Recipe {
766
769
  */
767
770
  constructor(content) {
768
771
  /**
769
- * The recipe's metadata.
770
- * @see {@link Metadata}
772
+ * The parsed recipe metadata.
771
773
  */
772
774
  __publicField(this, "metadata", {});
773
775
  /**
774
- * The recipe's ingredients.
775
- * @see {@link Ingredient}
776
+ * The parsed recipe ingredients.
776
777
  */
777
778
  __publicField(this, "ingredients", []);
778
779
  /**
779
- * The recipe's sections.
780
- * @see {@link Section}
780
+ * The parsed recipe sections.
781
781
  */
782
782
  __publicField(this, "sections", []);
783
783
  /**
784
- * The recipe's cookware.
785
- * @see {@link Cookware}
784
+ * The parsed recipe cookware.
786
785
  */
787
786
  __publicField(this, "cookware", []);
788
787
  /**
789
- * The recipe's timers.
790
- * @see {@link Timer}
788
+ * The parsed recipe timers.
791
789
  */
792
790
  __publicField(this, "timers", []);
793
791
  /**
794
- * The recipe's servings. Used for scaling
792
+ * The parsed recipe servings. Used for scaling. Parsed from one of
793
+ * {@link Metadata.servings}, {@link Metadata.yield} or {@link Metadata.serves}
794
+ * metadata fields.
795
+ *
796
+ * @see {@link Recipe.scaleBy | scaleBy()} and {@link Recipe.scaleTo | scaleTo()} methods
795
797
  */
796
798
  __publicField(this, "servings");
797
799
  if (content) {
@@ -953,9 +955,12 @@ var Recipe = class _Recipe {
953
955
  }
954
956
  }
955
957
  /**
956
- * Scales the recipe to a new number of servings.
958
+ * Scales the recipe to a new number of servings. In practice, it calls
959
+ * {@link Recipe.scaleBy | scaleBy} with a factor corresponding to the ratio between `newServings`
960
+ * and the recipe's {@link Recipe.servings | servings} value.
957
961
  * @param newServings - The new number of servings.
958
962
  * @returns A new Recipe instance with the scaled ingredients.
963
+ * @throws `Error` if the recipe does not contains an initial {@link Recipe.servings | servings} value
959
964
  */
960
965
  scaleTo(newServings) {
961
966
  const originalServings = this.getServings();
@@ -1040,32 +1045,28 @@ var Recipe = class _Recipe {
1040
1045
  // src/classes/shopping_list.ts
1041
1046
  var ShoppingList = class {
1042
1047
  /**
1043
- * Creates a new ShoppingList instance.
1044
- * @param aisle_config_str - The aisle configuration to parse.
1048
+ * Creates a new ShoppingList instance
1049
+ * @param category_config_str - The category configuration to parse.
1045
1050
  */
1046
- constructor(aisle_config_str) {
1051
+ constructor(category_config_str) {
1047
1052
  /**
1048
1053
  * The ingredients in the shopping list.
1049
- * @see {@link Ingredient}
1050
1054
  */
1051
1055
  __publicField(this, "ingredients", []);
1052
1056
  /**
1053
1057
  * The recipes in the shopping list.
1054
- * @see {@link AddedRecipe}
1055
1058
  */
1056
1059
  __publicField(this, "recipes", []);
1057
1060
  /**
1058
- * The aisle configuration for the shopping list.
1059
- * @see {@link AisleConfig}
1061
+ * The category configuration for the shopping list.
1060
1062
  */
1061
- __publicField(this, "aisle_config");
1063
+ __publicField(this, "category_config");
1062
1064
  /**
1063
1065
  * The categorized ingredients in the shopping list.
1064
- * @see {@link CategorizedIngredients}
1065
1066
  */
1066
1067
  __publicField(this, "categories");
1067
- if (aisle_config_str) {
1068
- this.set_aisle_config(aisle_config_str);
1068
+ if (category_config_str) {
1069
+ this.set_category_config(category_config_str);
1069
1070
  }
1070
1071
  }
1071
1072
  calculate_ingredients() {
@@ -1121,7 +1122,8 @@ var ShoppingList = class {
1121
1122
  }
1122
1123
  }
1123
1124
  /**
1124
- * Adds a recipe to the shopping list.
1125
+ * Adds a recipe to the shopping list, then automatically
1126
+ * recalculates the quantities and recategorize the ingredients.
1125
1127
  * @param recipe - The recipe to add.
1126
1128
  * @param factor - The factor to scale the recipe by.
1127
1129
  */
@@ -1131,7 +1133,8 @@ var ShoppingList = class {
1131
1133
  this.categorize();
1132
1134
  }
1133
1135
  /**
1134
- * Removes a recipe from the shopping list.
1136
+ * Removes a recipe from the shopping list, then automatically
1137
+ * recalculates the quantities and recategorize the ingredients.s
1135
1138
  * @param index - The index of the recipe to remove.
1136
1139
  */
1137
1140
  remove_recipe(index) {
@@ -1143,31 +1146,35 @@ var ShoppingList = class {
1143
1146
  this.categorize();
1144
1147
  }
1145
1148
  /**
1146
- * Sets the aisle configuration for the shopping list.
1147
- * @param config - The aisle configuration to parse.
1149
+ * Sets the category configuration for the shopping list
1150
+ * and automatically categorize current ingredients from the list.
1151
+ * @param config - The category configuration to parse.
1148
1152
  */
1149
- set_aisle_config(config) {
1150
- this.aisle_config = new AisleConfig(config);
1153
+ set_category_config(config) {
1154
+ if (typeof config === "string")
1155
+ this.category_config = new CategoryConfig(config);
1156
+ else if (config instanceof CategoryConfig) this.category_config = config;
1157
+ else throw new Error("Invalid category configuration");
1151
1158
  this.categorize();
1152
1159
  }
1153
1160
  /**
1154
1161
  * Categorizes the ingredients in the shopping list
1155
- * Will use the aisle config if any, otherwise all ingredients will be placed in the "other" category
1162
+ * Will use the category config if any, otherwise all ingredients will be placed in the "other" category
1156
1163
  */
1157
1164
  categorize() {
1158
- if (!this.aisle_config) {
1165
+ if (!this.category_config) {
1159
1166
  this.categories = { other: this.ingredients };
1160
1167
  return;
1161
1168
  }
1162
1169
  const categories = { other: [] };
1163
- for (const category of this.aisle_config.categories) {
1170
+ for (const category of this.category_config.categories) {
1164
1171
  categories[category.name] = [];
1165
1172
  }
1166
1173
  for (const ingredient of this.ingredients) {
1167
1174
  let found = false;
1168
- for (const category of this.aisle_config.categories) {
1169
- for (const aisleIngredient of category.ingredients) {
1170
- if (aisleIngredient.aliases.includes(ingredient.name)) {
1175
+ for (const category of this.category_config.categories) {
1176
+ for (const categoryIngredient of category.ingredients) {
1177
+ if (categoryIngredient.aliases.includes(ingredient.name)) {
1171
1178
  categories[category.name].push(ingredient);
1172
1179
  found = true;
1173
1180
  break;
@@ -1185,7 +1192,7 @@ var ShoppingList = class {
1185
1192
  }
1186
1193
  };
1187
1194
  export {
1188
- AisleConfig,
1195
+ CategoryConfig,
1189
1196
  Recipe,
1190
1197
  Section,
1191
1198
  ShoppingList