@tmlmt/cooklang-parser 2.0.0 → 2.0.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
@@ -1,11 +1,16 @@
1
1
  /**
2
2
  * Represents a recipe section
3
- * Wrapped as a Class and not as a simple type to expose some useful helper
4
- * classes (e.g. `isBlank()`)
3
+ *
4
+ * Wrapped as a _Class_ and not defined as a simple _Type_ to expose some useful helper
5
+ * classes (e.g. {@link Section.isBlank | isBlank()})
6
+ *
5
7
  * @category Types
6
8
  */
7
9
  declare class Section {
8
- /** The name of the section. Can be an empty string for the default (first) section. */
10
+ /**
11
+ * The name of the section. Can be an empty string for the default (first) section.
12
+ * @defaultValue `""`
13
+ */
9
14
  name: string;
10
15
  /** An array of steps and notes that make up the content of the section. */
11
16
  content: (Step | Note)[];
@@ -23,37 +28,63 @@ declare class Section {
23
28
  }
24
29
 
25
30
  /**
26
- * Represents a recipe.
31
+ * Recipe parser.
32
+ *
33
+ * ## Usage
34
+ *
35
+ * You can either directly provide the recipe string when creating the instance
36
+ * e.g. `const recipe = new Recipe('Add @eggs{3}')`, or create it first and then pass
37
+ * the recipe string to the {@link Recipe.parse | parse()} method.
38
+ *
39
+ * Look at the [properties](#properties) to see how the recipe's properties are parsed.
40
+ *
27
41
  * @category Classes
42
+ *
43
+ * @example
44
+ * ```typescript
45
+ * import { Recipe } from "@tmlmt/cooklang-parser";
46
+ *
47
+ * const recipeString = `
48
+ * ---
49
+ * title: Pancakes
50
+ * tags: [breakfast, easy]
51
+ * ---
52
+ * Crack the @eggs{3} with @flour{100%g} and @milk{200%mL}
53
+ *
54
+ * Melt some @butter{50%g} in a #pan on medium heat.
55
+ *
56
+ * Cook for ~{5%minutes} on each side.
57
+ * `
58
+ * const recipe = new Recipe(recipeString);
59
+ * ```
28
60
  */
29
61
  declare class Recipe {
30
62
  /**
31
- * The recipe's metadata.
32
- * @see {@link Metadata}
63
+ * The parsed recipe metadata.
33
64
  */
34
65
  metadata: Metadata;
35
66
  /**
36
- * The recipe's ingredients.
37
- * @see {@link Ingredient}
67
+ * The parsed recipe ingredients.
38
68
  */
39
69
  ingredients: Ingredient[];
40
70
  /**
41
- * The recipe's sections.
42
- * @see {@link Section}
71
+ * The parsed recipe sections.
43
72
  */
44
73
  sections: Section[];
45
74
  /**
46
- * The recipe's cookware.
47
- * @see {@link Cookware}
75
+ * The parsed recipe cookware.
48
76
  */
49
77
  cookware: Cookware[];
50
78
  /**
51
- * The recipe's timers.
52
- * @see {@link Timer}
79
+ * The parsed recipe timers.
53
80
  */
54
81
  timers: Timer[];
55
82
  /**
56
- * The recipe's servings. Used for scaling
83
+ * The parsed recipe servings. Used for scaling. Parsed from one of
84
+ * {@link Metadata.servings}, {@link Metadata.yield} or {@link Metadata.serves}
85
+ * metadata fields.
86
+ *
87
+ * @see {@link Recipe.scaleBy | scaleBy()} and {@link Recipe.scaleTo | scaleTo()} methods
57
88
  */
58
89
  servings?: number;
59
90
  /**
@@ -67,9 +98,12 @@ declare class Recipe {
67
98
  */
68
99
  parse(content: string): void;
69
100
  /**
70
- * Scales the recipe to a new number of servings.
101
+ * Scales the recipe to a new number of servings. In practice, it calls
102
+ * {@link Recipe.scaleBy | scaleBy} with a factor corresponding to the ratio between `newServings`
103
+ * and the recipe's {@link Recipe.servings | servings} value.
71
104
  * @param newServings - The new number of servings.
72
105
  * @returns A new Recipe instance with the scaled ingredients.
106
+ * @throws `Error` if the recipe does not contains an initial {@link Recipe.servings | servings} value
73
107
  */
74
108
  scaleTo(newServings: number): Recipe;
75
109
  /**
@@ -91,6 +125,11 @@ declare class Recipe {
91
125
  clone(): Recipe;
92
126
  }
93
127
 
128
+ interface Quantity {
129
+ value: FixedValue | Range;
130
+ unit?: string;
131
+ }
132
+
94
133
  /**
95
134
  * Represents the metadata of a recipe.
96
135
  * @category Types
@@ -102,26 +141,62 @@ interface Metadata {
102
141
  tags?: string[];
103
142
  /** The source of the recipe. */
104
143
  source?: string;
144
+ /** The source name of the recipe. */
145
+ "source.name"?: string;
146
+ /** The source url of the recipe. */
147
+ "source.url"?: string;
148
+ /** The source author of the recipe. */
149
+ "source.author"?: string;
105
150
  /** The author of the recipe. */
106
151
  author?: string;
107
152
  /** The number of servings the recipe makes.
108
- * Complex info can be given, as long as the first part before a comma has a numerical value, which will be used for scaling
109
- * Interchangeable with `yield` or `serves`. If multiple ones are defined, the latest one will be used for scaling */
110
- servings?: string;
153
+ * Should be either a number or a string which starts with a number
154
+ * (which will be used for scaling) followed by a comma and then
155
+ * whatever you want.
156
+ *
157
+ * Interchangeable with `yield` or `serves`. If multiple ones are defined,
158
+ * the prevailance order for the number which will used for scaling
159
+ * is `servings` \> `yield` \> `serves`.
160
+ *
161
+ * @example
162
+ * ```yaml
163
+ * servings: 4
164
+ * ```
165
+ *
166
+ * @example
167
+ * ```yaml
168
+ * servings: 2, a few
169
+ * ```
170
+ */
171
+ servings?: number | string;
111
172
  /** The yield of the recipe.
112
- * Complex info can be given, as long as the first part before a comma has a numerical value, which will be used for scaling
113
- * Interchangeable with `servings` or `serves`. If multiple ones are defined, the latest one will be used for scaling
114
- */
115
- yield?: string;
173
+ * Should be either a number or a string which starts with a number
174
+ * (which will be used for scaling) followed by a comma and then
175
+ * whatever you want.
176
+ *
177
+ * Interchangeable with `servings` or `serves`. If multiple ones are defined,
178
+ * the prevailance order for the number which will used for scaling
179
+ * is `servings` \> `yield` \> `serves`. See {@link Metadata.servings | servings}
180
+ * for examples.
181
+ */
182
+ yield?: number | string;
116
183
  /** The number of people the recipe serves.
117
- * Complex info can be given, as long as the first part before a comma has a numerical value, which will be used for scaling
118
- * Interchangeable with `servings` or `yield`. If multiple ones are defined, the latest one will be used for scaling
119
- */
120
- serves?: string;
184
+ * Should be either a number or a string which starts with a number
185
+ * (which will be used for scaling) followed by a comma and then
186
+ * whatever you want.
187
+ *
188
+ * Interchangeable with `servings` or `yield`. If multiple ones are defined,
189
+ * the prevailance order for the number which will used for scaling
190
+ * is `servings` \> `yield` \> `serves`. See {@link Metadata.servings | servings}
191
+ * for examples.
192
+ */
193
+ serves?: number | string;
121
194
  /** The course of the recipe. */
122
195
  course?: string;
123
196
  /** The category of the recipe. */
124
197
  category?: string;
198
+ /** The locale of the recipe. */
199
+ locale?: string;
125
200
  /**
126
201
  * The preparation time of the recipe.
127
202
  * Will not be further parsed into any DateTime format nor normalize
@@ -155,18 +230,16 @@ interface Metadata {
155
230
  diet?: string;
156
231
  /** The description of the recipe. */
157
232
  description?: string;
158
- /** The images of the recipe. */
233
+ /** The images of the recipe. Alias of `pictures` */
159
234
  images?: string[];
160
- }
161
- /**
162
- * Represents the extracted metadata from a recipe.
163
- * @category Types
164
- */
165
- interface MetadataExtract {
166
- /** The metadata of the recipe. */
167
- metadata: Metadata;
168
- /** The number of servings the recipe makes. Used for scaling */
169
- servings?: number;
235
+ /** The images of the recipe. Alias of `images` */
236
+ pictures?: string[];
237
+ /** The picture of the recipe. Alias of `picture` */
238
+ image?: string;
239
+ /** The picture of the recipe. Alias of `image` */
240
+ picture?: string;
241
+ /** The introduction of the recipe. */
242
+ introduction?: string;
170
243
  }
171
244
  /**
172
245
  * Represents a quantity described by text, e.g. "a pinch"
@@ -213,6 +286,41 @@ interface Range {
213
286
  min: DecimalValue | FractionValue;
214
287
  max: DecimalValue | FractionValue;
215
288
  }
289
+ /**
290
+ * Represents a contributor to an ingredient's total quantity
291
+ * @category Types
292
+ */
293
+ interface QuantityPart extends Quantity {
294
+ /** - If _true_, the quantity will scale
295
+ * - If _false_, the quantity is fixed
296
+ */
297
+ scalable: boolean;
298
+ }
299
+ /**
300
+ * Represents a possible state modifier or other flag for an ingredient in a recipe
301
+ * @category Types
302
+ */
303
+ type IngredientFlag = "optional" | "hidden" | "recipe";
304
+ /**
305
+ * Represents the collection of possible additional metadata for an ingredient in a recipe
306
+ * @category Types
307
+ */
308
+ interface IngredientExtras {
309
+ /**
310
+ * The path of the ingredient-recipe, relative to the present recipe
311
+ * Used if: the ingredient is a recipe
312
+ *
313
+ * @example
314
+ * ```cooklang
315
+ * Take @./essentials/doughs/pizza dough{1} out of the freezer and let it unfreeze overnight
316
+ * ```
317
+ * Would lead to:
318
+ * ```yaml
319
+ * path: 'essentials/doughts/pizza dough.cook'
320
+ * ```
321
+ */
322
+ path: string;
323
+ }
216
324
  /**
217
325
  * Represents an ingredient in a recipe.
218
326
  * @category Types
@@ -224,14 +332,14 @@ interface Ingredient {
224
332
  quantity?: FixedValue | Range;
225
333
  /** The unit of the ingredient. */
226
334
  unit?: string;
335
+ /** The array of contributors to the ingredient's total quantity. */
336
+ quantityParts?: QuantityPart[];
227
337
  /** The preparation of the ingredient. */
228
338
  preparation?: string;
229
- /** Whether the ingredient is optional. */
230
- optional?: boolean;
231
- /** Whether the ingredient is hidden. */
232
- hidden?: boolean;
233
- /** Whether the ingredient is a recipe. */
234
- isRecipe?: boolean;
339
+ /** A list of potential state modifiers or other flags for the ingredient */
340
+ flags?: IngredientFlag[];
341
+ /** The collection of potential additional metadata for the ingredient */
342
+ extras?: IngredientExtras;
235
343
  }
236
344
  /**
237
345
  * Represents a timer in a recipe.
@@ -252,7 +360,7 @@ interface Timer {
252
360
  interface TextItem {
253
361
  /** The type of the item. */
254
362
  type: "text";
255
- /** The value of the item. */
363
+ /** The content of the text item. */
256
364
  value: string;
257
365
  }
258
366
  /**
@@ -262,14 +370,14 @@ interface TextItem {
262
370
  interface IngredientItem {
263
371
  /** The type of the item. */
264
372
  type: "ingredient";
265
- /** The value of the item. */
266
- value: number;
267
- /** The alias/name of the ingredient as it should be displayed in the preparation */
373
+ /** The index of the ingredient, within the {@link Recipe.ingredients | list of ingredients} */
374
+ index: number;
375
+ /** Index of the quantity part corresponding to this item / this occurence
376
+ * of the ingredient, which may be referenced elsewhere. */
377
+ quantityPartIndex?: number;
378
+ /** The alias/name of the ingredient as it should be displayed in the preparation
379
+ * for this occurence */
268
380
  displayName: string;
269
- /** Quantity specific to this step item for this ingredient which may also be referenced elsewhere */
270
- itemQuantity?: FixedValue | Range;
271
- /** Unit specific to this step item for this ingredient which may also be referenced elsewhere */
272
- itemUnit?: string;
273
381
  }
274
382
  /**
275
383
  * Represents a cookware item in a recipe step.
@@ -278,10 +386,11 @@ interface IngredientItem {
278
386
  interface CookwareItem {
279
387
  /** The type of the item. */
280
388
  type: "cookware";
281
- /** The value of the item. */
282
- value: number;
283
- /** Quantity specific to this step item for this cookware which may also be referenced elsewhere */
284
- itemQuantity?: FixedValue | Range;
389
+ /** The index of the cookware, within the {@link Recipe.cookware | list of cookware} */
390
+ index: number;
391
+ /** Index of the quantity part corresponding to this item / this occurence
392
+ * of the cookware, which may be referenced elsewhere. */
393
+ quantityPartIndex?: number;
285
394
  }
286
395
  /**
287
396
  * Represents a timer item in a recipe step.
@@ -290,8 +399,8 @@ interface CookwareItem {
290
399
  interface TimerItem {
291
400
  /** The type of the item. */
292
401
  type: "timer";
293
- /** The value of the item. */
294
- value: number;
402
+ /** The index of the timer, within the {@link Recipe.timers | list of timers} */
403
+ index: number;
295
404
  }
296
405
  /**
297
406
  * Represents an item in a recipe step.
@@ -316,6 +425,11 @@ interface Note {
316
425
  /** The content of the note. */
317
426
  note: string;
318
427
  }
428
+ /**
429
+ * Represents a possible state modifier or other flag for cookware used in a recipe
430
+ * @category Types
431
+ */
432
+ type CookwareFlag = "optional" | "hidden";
319
433
  /**
320
434
  * Represents a piece of cookware in a recipe.
321
435
  * @category Types
@@ -325,17 +439,16 @@ interface Cookware {
325
439
  name: string;
326
440
  /** The quantity of cookware */
327
441
  quantity?: FixedValue | Range;
328
- /** Whether the cookware is optional. */
329
- optional?: boolean;
330
- /** Whether the cookware is hidden. */
331
- hidden?: boolean;
442
+ /** The array of contributors to the cookware's total quantity. */
443
+ quantityParts?: (FixedValue | Range)[];
444
+ /** A list of potential state modifiers or other flags for the cookware */
445
+ flags: CookwareFlag[];
332
446
  }
333
447
  /**
334
448
  * Represents categorized ingredients.
335
449
  * @category Types
336
450
  */
337
451
  interface CategorizedIngredients {
338
- /** The category of the ingredients. */
339
452
  [category: string]: Ingredient[];
340
453
  }
341
454
  /**
@@ -349,100 +462,148 @@ interface AddedRecipe {
349
462
  factor: number;
350
463
  }
351
464
  /**
352
- * Represents an ingredient in an aisle.
465
+ * Represents an ingredient in a category.
353
466
  * @category Types
354
467
  */
355
- interface AisleIngredient {
468
+ interface CategoryIngredient {
356
469
  /** The name of the ingredient. */
357
470
  name: string;
358
471
  /** The aliases of the ingredient. */
359
472
  aliases: string[];
360
473
  }
361
474
  /**
362
- * Represents a category of aisles.
475
+ * Represents a category of ingredients.
363
476
  * @category Types
364
477
  */
365
- interface AisleCategory {
478
+ interface Category {
366
479
  /** The name of the category. */
367
480
  name: string;
368
481
  /** The ingredients in the category. */
369
- ingredients: AisleIngredient[];
482
+ ingredients: CategoryIngredient[];
370
483
  }
371
484
 
372
485
  /**
373
- * Represents the aisle configuration for a shopping list.
486
+ * Parser for category configurations specified à-la-cooklang.
487
+ *
488
+ * ## Usage
489
+ *
490
+ * You can either directly provide the category configuration string when creating the instance
491
+ * e.g. `const categoryConfig = new CategoryConfig(<...>)`, or create it first and then pass
492
+ * the category configuration string to the {@link CategoryConfig.parse | parse()} method.
493
+ *
494
+ * The initialized `CategoryConfig` can then be fed to a {@link ShoppingList}
495
+ *
496
+ * @example
497
+ * ```typescript
498
+ * import { CategoryConfig } from @tmlmt/cooklang-parser;
499
+ *
500
+ * const categoryConfigString = `
501
+ * [Dairy]
502
+ * milk
503
+ * butter
504
+ *
505
+ * [Bakery]
506
+ * flour
507
+ * sugar`;
508
+ *
509
+ * const categoryConfig = new CategoryConfig(categoryConfigString);
510
+ * ```
511
+ *
512
+ * @see [Category Configuration](https://cooklang.org/docs/spec/#shopping-lists) section of the cooklang specs
513
+ *
374
514
  * @category Classes
375
515
  */
376
- declare class AisleConfig {
516
+ declare class CategoryConfig {
377
517
  /**
378
- * The categories of aisles.
379
- * @see {@link AisleCategory}
518
+ * The parsed categories of ingredients.
380
519
  */
381
- categories: AisleCategory[];
520
+ categories: Category[];
382
521
  /**
383
- * Creates a new AisleConfig instance.
384
- * @param config - The aisle configuration to parse.
522
+ * Creates a new CategoryConfig instance.
523
+ * @param config - The category configuration to parse.
385
524
  */
386
525
  constructor(config?: string);
387
526
  /**
388
- * Parses an aisle configuration from a string.
389
- * @param config - The aisle configuration to parse.
527
+ * Parses a category configuration from a string into property
528
+ * {@link CategoryConfig.categories | categories}
529
+ * @param config - The category configuration to parse.
390
530
  */
391
531
  parse(config: string): void;
392
532
  }
393
533
 
394
534
  /**
395
- * Represents a shopping list.
535
+ * Shopping List generator.
536
+ *
537
+ * ## Usage
538
+ *
539
+ * - Create a new ShoppingList instance with an optional category configuration (see {@link ShoppingList."constructor" | constructor})
540
+ * - Add recipes, scaling them as needed (see {@link ShoppingList.add_recipe | add_recipe()})
541
+ * - Categorize the ingredients (see {@link ShoppingList.categorize | categorize()})
542
+ *
543
+ * @example
544
+ *
545
+ * ```typescript
546
+ * import * as fs from "fs";
547
+ * import { ShoppingList } from @tmlmt/cooklang-parser;
548
+ *
549
+ * const categoryConfig = fs.readFileSync("./myconfig.txt", "utf-8")
550
+ * const recipe1 = new Recipe(fs.readFileSync("./myrecipe.cook", "utf-8"));
551
+ * const shoppingList = new ShoppingList();
552
+ * shoppingList.set_category_config(categoryConfig);
553
+ * // Quantities are automatically calculated and ingredients categorized
554
+ * // when adding a recipe
555
+ * shoppingList.add_recipe(recipe1);
556
+ * ```
557
+ *
396
558
  * @category Classes
397
559
  */
398
560
  declare class ShoppingList {
399
561
  /**
400
562
  * The ingredients in the shopping list.
401
- * @see {@link Ingredient}
402
563
  */
403
564
  ingredients: Ingredient[];
404
565
  /**
405
566
  * The recipes in the shopping list.
406
- * @see {@link AddedRecipe}
407
567
  */
408
568
  recipes: AddedRecipe[];
409
569
  /**
410
- * The aisle configuration for the shopping list.
411
- * @see {@link AisleConfig}
570
+ * The category configuration for the shopping list.
412
571
  */
413
- aisle_config?: AisleConfig;
572
+ category_config?: CategoryConfig;
414
573
  /**
415
574
  * The categorized ingredients in the shopping list.
416
- * @see {@link CategorizedIngredients}
417
575
  */
418
576
  categories?: CategorizedIngredients;
419
577
  /**
420
- * Creates a new ShoppingList instance.
421
- * @param aisle_config_str - The aisle configuration to parse.
578
+ * Creates a new ShoppingList instance
579
+ * @param category_config_str - The category configuration to parse.
422
580
  */
423
- constructor(aisle_config_str?: string);
581
+ constructor(category_config_str?: string | CategoryConfig);
424
582
  private calculate_ingredients;
425
583
  /**
426
- * Adds a recipe to the shopping list.
584
+ * Adds a recipe to the shopping list, then automatically
585
+ * recalculates the quantities and recategorize the ingredients.
427
586
  * @param recipe - The recipe to add.
428
587
  * @param factor - The factor to scale the recipe by.
429
588
  */
430
589
  add_recipe(recipe: Recipe, factor?: number): void;
431
590
  /**
432
- * Removes a recipe from the shopping list.
591
+ * Removes a recipe from the shopping list, then automatically
592
+ * recalculates the quantities and recategorize the ingredients.s
433
593
  * @param index - The index of the recipe to remove.
434
594
  */
435
595
  remove_recipe(index: number): void;
436
596
  /**
437
- * Sets the aisle configuration for the shopping list.
438
- * @param config - The aisle configuration to parse.
597
+ * Sets the category configuration for the shopping list
598
+ * and automatically categorize current ingredients from the list.
599
+ * @param config - The category configuration to parse.
439
600
  */
440
- set_aisle_config(config: string): void;
601
+ set_category_config(config: string | CategoryConfig): void;
441
602
  /**
442
603
  * Categorizes the ingredients in the shopping list
443
- * Will use the aisle config if any, otherwise all ingredients will be placed in the "other" category
604
+ * Will use the category config if any, otherwise all ingredients will be placed in the "other" category
444
605
  */
445
606
  categorize(): void;
446
607
  }
447
608
 
448
- export { type AddedRecipe, type AisleCategory, AisleConfig, type AisleIngredient, type CategorizedIngredients, type Cookware, type CookwareItem, type DecimalValue, type FixedValue, type FractionValue, type Ingredient, type IngredientItem, type Item, type Metadata, type MetadataExtract, type Note, type Range, Recipe, Section, ShoppingList, type Step, type TextItem, type TextValue, type Timer, type TimerItem };
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 };