@tmlmt/cooklang-parser 3.0.0-alpha.17 → 3.0.0-alpha.20

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
@@ -16,11 +16,17 @@ declare class Section {
16
16
  name: string;
17
17
  /** An array of steps and notes that make up the content of the section. */
18
18
  content: (Step | Note)[];
19
+ /** Optional list of variant names this section belongs to. */
20
+ variants?: string[];
21
+ /** Whether the section has been marked as optional ([?]) */
22
+ optional?: boolean;
19
23
  /**
20
24
  * Creates an instance of Section.
21
25
  * @param name - The name of the section. Defaults to an empty string.
26
+ * @param variants - Optional variant names for this section.
27
+ * @param optional - Whether the section is optional.
22
28
  */
23
- constructor(name?: string);
29
+ constructor(name?: string, variants?: string[], optional?: boolean);
24
30
  /**
25
31
  * Checks if the section is blank (has no name and no content).
26
32
  * Used during recipe parsing
@@ -207,6 +213,25 @@ declare class Recipe {
207
213
  * ```
208
214
  */
209
215
  getIngredientQuantities(options?: GetIngredientQuantitiesOptions): Ingredient[];
216
+ /**
217
+ * Returns the list of cookware items that are used in the active variant.
218
+ * Cookware in steps/sections not matching the active variant are excluded.
219
+ * Hidden cookware is always excluded.
220
+ *
221
+ * @param options - Options for filtering:
222
+ * - `choices`: The choices to apply (only `variant` is used)
223
+ * @returns Array of Cookware objects referenced by active steps
224
+ *
225
+ * @example
226
+ * ```typescript
227
+ * // Get all cookware for the default variant
228
+ * const cookware = recipe.getCookwareForVariant();
229
+ *
230
+ * // Get cookware for a specific variant
231
+ * const veganCookware = recipe.getCookwareForVariant({ choices: { variant: 'vegan' } });
232
+ * ```
233
+ */
234
+ getCookwareForVariant(options?: Pick<GetIngredientQuantitiesOptions, "choices">): Cookware[];
210
235
  /**
211
236
  * Parses a recipe from a string.
212
237
  * @param content - The recipe content to parse.
@@ -274,16 +299,17 @@ interface MetadataSource {
274
299
  author?: string;
275
300
  }
276
301
  /**
277
- * Represents scaling variable information for a recipe.
302
+ * Represents a yield metadata value for a recipe.
303
+ * Supports plain quantities (e.g. `yield: 300%g`), complex format with
304
+ * surrounding text (e.g. `yield: about {{300%g}} of bread`), or simple
305
+ * numbers (e.g. `yield: 2`).
278
306
  * @category Types
279
307
  */
280
- interface MetadataScalingVar extends QuantityWithPlainUnit {
281
- /** The text before the scaling variable. */
308
+ interface Yield extends QuantityWithPlainUnit {
309
+ /** The text before the scaling variable (complex `{{}}` format). */
282
310
  textBefore?: string;
283
- /** The text after the scaling variable. */
311
+ /** The text after the scaling variable (complex `{{}}` format). */
284
312
  textAfter?: string;
285
- /** The text precising a numerical scaling variable. */
286
- text?: string;
287
313
  }
288
314
  /**
289
315
  * Represents time information for a recipe.
@@ -308,7 +334,7 @@ interface MetadataObject {
308
334
  * Represents any value that can appear in recipe metadata.
309
335
  * @category Types
310
336
  */
311
- type MetadataValue = string | number | (string | number)[] | MetadataObject | MetadataSource | MetadataTime | MetadataScalingVar | undefined;
337
+ type MetadataValue = string | number | (string | number)[] | MetadataObject | MetadataSource | MetadataTime | Yield | undefined;
312
338
  /**
313
339
  * Represents the metadata of a recipe.
314
340
  * @category Types
@@ -326,55 +352,54 @@ interface Metadata {
326
352
  /** The author of the recipe (separate from source author). */
327
353
  author?: string;
328
354
  /** The number of servings the recipe makes.
329
- * Can be given either as:
330
- * - a number
331
- * - a string which starts with a number (which will be used for scaling) followed by a comma and then whatever you want
332
- * - or an arbitrary scalable number, optionally preceded and/or followed by text.
355
+ * Stored as a number when the value is numeric, or as a string when non-numeric
356
+ * (e.g. `servings: two`). Non-numeric values default the internal scaling baseline to 1.
333
357
  *
334
- * Interchangeable with `yield` or `serves`. If multiple ones are defined,
335
- * the prevailance order for the number which will used for scaling
336
- * is `servings` \> `yield` \> `serves`.
358
+ * Interchangeable with `yield` or `serves` for the purpose of setting
359
+ * {@link Recipe.servings}. If multiple ones are defined, the prevalence
360
+ * order is `servings` \> `serves` \> `yield`.
337
361
  *
338
362
  * @example
339
363
  * ```yaml
340
364
  * servings: 4
341
365
  * ```
366
+ */
367
+ servings?: number | string;
368
+ /** The yield of the recipe.
369
+ * Can be given as:
370
+ * - a plain quantity with optional unit: `yield: 300%g` or `yield: 2`
371
+ * - a complex format with `{{}}` and optional surrounding text:
372
+ * `yield: about {{300%g}} of bread`
373
+ *
374
+ * Interchangeable with `servings` or `serves` for the purpose of setting
375
+ * {@link Recipe.servings}. If multiple ones are defined, the prevalence
376
+ * order is `servings` \> `serves` \> `yield`.
342
377
  *
343
378
  * @example
344
379
  * ```yaml
345
- * servings: 2, a few
380
+ * yield: 300%g
346
381
  * ```
347
382
  *
348
- * * @example
383
+ * @example
349
384
  * ```yaml
350
- * servings: {{1.5%kg}} of bread
385
+ * yield: about {{300%g}} of bread
351
386
  * ```
352
387
  */
353
- servings?: MetadataScalingVar;
354
- /** The yield of the recipe.
355
- * Can be given either as:
356
- * - a number
357
- * - a string which starts with a number (which will be used for scaling) followed by a comma and then whatever you want
358
- * - or an arbitrary scalable number, optionally preceded and/or followed by text.
359
- *
360
- * Interchangeable with `servings` or `serves`. If multiple ones are defined,
361
- * the prevailance order for the number which will used for scaling
362
- * is `servings` \> `yield` \> `serves`. See {@link Metadata.servings | servings}
363
- * for examples.
364
- */
365
- yield?: MetadataScalingVar;
388
+ yield?: Yield;
366
389
  /** The number of people the recipe serves.
367
- * Can be given either as:
368
- * - a number
369
- * - a string which starts with a number (which will be used for scaling) followed by a comma and then whatever you want
370
- * - or an arbitrary scalable number, optionally preceded and/or followed by text.
390
+ * Stored as a number when the value is numeric, or as a string when non-numeric
391
+ * (e.g. `serves: two`). Non-numeric values default the internal scaling baseline to 1.
392
+ *
393
+ * Interchangeable with `servings` or `yield` for the purpose of setting
394
+ * {@link Recipe.servings}. If multiple ones are defined, the prevalence
395
+ * order is `servings` \> `serves` \> `yield`.
371
396
  *
372
- * Interchangeable with `servings` or `yield`. If multiple ones are defined,
373
- * the prevailance order for the number which will used for scaling
374
- * is `servings` \> `yield` \> `serves`. See {@link Metadata.servings | servings}
375
- * for examples.
397
+ * @example
398
+ * ```yaml
399
+ * serves: 4
400
+ * ```
376
401
  */
377
- serves?: MetadataScalingVar;
402
+ serves?: number | string;
378
403
  /** The course of the recipe. */
379
404
  course?: string;
380
405
  /** The category of the recipe. */
@@ -407,6 +432,8 @@ interface Metadata {
407
432
  * This stores the original value as written by the user.
408
433
  */
409
434
  unitSystem?: string;
435
+ /** The list of available variant names for this recipe. */
436
+ variants?: string[];
410
437
  /**
411
438
  * Index signature for additional metadata fields not explicitly typed.
412
439
  * Any metadata key in the frontmatter will be captured here.
@@ -567,6 +594,12 @@ interface Ingredient {
567
594
  /** The collection of potential additional metadata for the ingredient */
568
595
  extras?: IngredientExtras;
569
596
  }
597
+ /**
598
+ * Represents a quantity with extended unit and additional information
599
+ * about its scalability and potential equivalents
600
+ *
601
+ * @category Types
602
+ */
570
603
  type MaybeScalableQuantity = QuantityWithExtendedUnit & {
571
604
  /** Indicates whether this quantity should be scaled when the recipe serving size changes. */
572
605
  scalable: boolean;
@@ -574,6 +607,15 @@ type MaybeScalableQuantity = QuantityWithExtendedUnit & {
574
607
  * For `@salt{1%tsp|5%g}`, the main quantity is 1 tsp and the equivalents will contain 5 g. */
575
608
  equivalents?: QuantityWithExtendedUnit[];
576
609
  };
610
+ /**
611
+ * Defines a type containing an optional {@link QuantityWithExtendedUnit} with scalability and potential info about equivalents
612
+ *
613
+ * Used in {@link IngredientAlternative}
614
+ *
615
+ * @param T - The base type to which the optional quantity properties will be added.
616
+ *
617
+ * @category Types
618
+ */
577
619
  type WithOptionalQuantity<T> = (T & MaybeScalableQuantity) | (T & {
578
620
  quantity?: undefined;
579
621
  scalable?: never;
@@ -581,8 +623,7 @@ type WithOptionalQuantity<T> = (T & MaybeScalableQuantity) | (T & {
581
623
  equivalents?: never;
582
624
  });
583
625
  /**
584
- * Represents a single ingredient choice within a single or a group of `IngredientItem`s. It points
585
- * to a specific ingredient and its corresponding quantity information.
626
+ * Base type of {@link IngredientAlternative}
586
627
  * @category Types
587
628
  */
588
629
  type IngredientAlternativeBase = {
@@ -597,6 +638,14 @@ type IngredientAlternativeBase = {
597
638
  * Can be useful for creating alternative selection UI elements with anchor links */
598
639
  itemId?: string;
599
640
  };
641
+ /**
642
+ * Represents a single ingredient choice within a single or a group of `IngredientItem`s. It points
643
+ * to a specific ingredient and its corresponding quantity information.
644
+ *
645
+ * Used in {@link IngredientItem} and for the various maps of {@link RecipeAlternatives}
646
+ *
647
+ * @category Types
648
+ */
600
649
  type IngredientAlternative = WithOptionalQuantity<IngredientAlternativeBase>;
601
650
  /**
602
651
  * Represents an ingredient item in a recipe step.
@@ -644,6 +693,11 @@ interface RecipeAlternatives {
644
693
  * their own single-element subgroup.
645
694
  */
646
695
  ingredientGroups: Map<string, IngredientAlternative[][]>;
696
+ /**
697
+ * All variant names discovered in the recipe (from metadata, step tags,
698
+ * and section tags). Includes `*` if any step/section uses the default tag.
699
+ */
700
+ variants: string[];
647
701
  }
648
702
  /**
649
703
  * Represents the choices to apply when computing ingredient quantities.
@@ -655,6 +709,8 @@ interface RecipeChoices {
655
709
  ingredientItems?: Map<string, number>;
656
710
  /** Map of choices that can be made for Grouped Ingredient StepItem's */
657
711
  ingredientGroups?: Map<string, number>;
712
+ /** The selected variant name */
713
+ variant?: string;
658
714
  }
659
715
  /**
660
716
  * Options for the {@link Recipe.getIngredientQuantities | getIngredientQuantities()} method.
@@ -679,9 +735,9 @@ interface GetIngredientQuantitiesOptions {
679
735
  }
680
736
  /**
681
737
  * Represents a raw (unprocessed) group of quantities for a single ingredient.
682
- * Returned by {@link Recipe.getRawQuantityGroups | getRawQuantityGroups()},
683
- * these are the pre-addition quantities that can be fed directly into
684
- * {@link addEquivalentsAndSimplify} for cross-recipe aggregation.
738
+ * Returned by {@link Recipe.getRawQuantityGroups},
739
+ * these are the pre-addition quantities that are fed internally
740
+ * to another non-public helper function for cross-recipe aggregation.
685
741
  * @category Types
686
742
  */
687
743
  interface RawQuantityGroup {
@@ -782,6 +838,10 @@ interface Step {
782
838
  type: "step";
783
839
  /** The items in the step. */
784
840
  items: StepItem[];
841
+ /** Optional list of variant names this step belongs to */
842
+ variants?: string[];
843
+ /** Whether the step has been marked as optional ("[?]") */
844
+ optional?: boolean;
785
845
  }
786
846
  /**
787
847
  * Represents an item in a note (can be text or arbitrary scalable).
@@ -852,6 +912,9 @@ interface RecipeWithServings {
852
912
  type AddedRecipe = RecipeWithFactor | RecipeWithServings;
853
913
  /**
854
914
  * Options for adding a recipe to a shopping list
915
+ *
916
+ * Used in {@link ShoppingList.addRecipe}
917
+ *
855
918
  * @category Types
856
919
  */
857
920
  type AddedRecipeOptions = {
@@ -1376,7 +1439,7 @@ declare class Pantry {
1376
1439
  * 14141 = { name = "Big pack", size = "6%kg", price = 10 }
1377
1440
  * `
1378
1441
  * const catalog = new ProductCatalog(catalog);
1379
- * const eggs = catalog.find("oeuf");
1442
+ * const eggs = catalog.products.find(p => p.ingredientName === "eggs");
1380
1443
  * ```
1381
1444
  */
1382
1445
  declare class ProductCatalog {
@@ -1480,19 +1543,6 @@ declare class ShoppingList {
1480
1543
  * and updates the resultingPantry to reflect consumed stock.
1481
1544
  */
1482
1545
  private applyPantrySubtraction;
1483
- /**
1484
- * Builds a ratio map from equivalence lists.
1485
- * For each equivalence list, stores ratio = equiv_value / primary_value
1486
- * for every pair of units, so equivalents can be recomputed after
1487
- * pantry subtraction modifies primary quantities.
1488
- */
1489
- private static buildEquivalenceRatioMap;
1490
- /**
1491
- * Recomputes equivalent quantities from current primary values and stored ratios.
1492
- * For each equivalent unit in equivUnits, new_value = Σ (primary_value × ratio[equivUnit][primaryUnit]).
1493
- * Returns undefined if all equivalents compute to zero.
1494
- */
1495
- private static recomputeEquivalents;
1496
1546
  /**
1497
1547
  * Adds a recipe to the shopping list, then automatically
1498
1548
  * recalculates the quantities and recategorize the ingredients.
@@ -1857,6 +1907,81 @@ declare function isGroupedItem(item: IngredientItem): boolean;
1857
1907
  * ```
1858
1908
  */
1859
1909
  declare function isAlternativeSelected(recipe: Recipe, choices: RecipeChoices, item: IngredientItem, alternativeIndex?: number): boolean;
1910
+ /**
1911
+ * Determines if a section is active (should be displayed or processed) for a given variant.
1912
+ *
1913
+ * - Sections with no `variants` property are always active.
1914
+ * - When no variant is selected (default), sections tagged `[*]` are active,
1915
+ * and sections tagged with named variants are not.
1916
+ * - When a named variant is selected, sections whose `variants` array includes
1917
+ * that name are active.
1918
+ *
1919
+ * @param section - The Section to check
1920
+ * @param variant - The active variant name, or `undefined`/`*` for the default variant
1921
+ * @returns `true` if the section should be displayed
1922
+ * @category Helpers
1923
+ *
1924
+ * @example
1925
+ * ```typescript
1926
+ * const recipe = new Recipe(cooklangText);
1927
+ * for (const section of recipe.sections) {
1928
+ * if (isSectionActive(section, choices.variant)) {
1929
+ * // render section
1930
+ * }
1931
+ * }
1932
+ * ```
1933
+ */
1934
+ declare function isSectionActive(section: Section, variant?: string): boolean;
1935
+ /**
1936
+ * Determines if a step is active (should be displayed) for a given variant.
1937
+ *
1938
+ * - Steps with no `variants` property are always active.
1939
+ * - When no variant is selected (default), steps tagged `[*]` are active,
1940
+ * and steps tagged with named variants are not.
1941
+ * - When a named variant is selected, steps whose `variants` array includes
1942
+ * that name are active.
1943
+ *
1944
+ * @param step - The Step to check
1945
+ * @param variant - The active variant name, or `undefined`/`*` for the default variant
1946
+ * @returns `true` if the step should be displayed
1947
+ * @category Helpers
1948
+ *
1949
+ * @example
1950
+ * ```typescript
1951
+ * for (const item of section.content) {
1952
+ * if (item.type === 'step' && isStepActive(item, choices.variant)) {
1953
+ * // render step
1954
+ * }
1955
+ * }
1956
+ * ```
1957
+ */
1958
+ declare function isStepActive(step: Step, variant?: string): boolean;
1959
+ /**
1960
+ * Returns the effective choices for a recipe given a variant selection.
1961
+ *
1962
+ * When a named variant is active, this scans ingredient alternatives whose
1963
+ * `note` contains the variant name (case-insensitive substring match) and
1964
+ * returns a `RecipeChoices` object with auto-selected alternatives.
1965
+ *
1966
+ * For inline alternatives: auto-selects the first alternative whose note
1967
+ * matches the variant name.
1968
+ *
1969
+ * For grouped alternatives: auto-selects the first subgroup that has any
1970
+ * alternative whose note matches the variant name.
1971
+ *
1972
+ * @param recipe - The Recipe instance
1973
+ * @param variant - The active variant name, or `undefined`/`*` for defaults
1974
+ * @returns A `RecipeChoices` with the `variant` set and auto-selected alternatives
1975
+ * @category Helpers
1976
+ *
1977
+ * @example
1978
+ * ```typescript
1979
+ * const recipe = new Recipe(cooklangText);
1980
+ * const choices = getEffectiveChoices(recipe, "vegan");
1981
+ * const ingredients = recipe.getIngredientQuantities({ choices });
1982
+ * ```
1983
+ */
1984
+ declare function getEffectiveChoices(recipe: Recipe, variant?: string): RecipeChoices;
1860
1985
 
1861
1986
  /**
1862
1987
  * Type guard to check if an ingredient quantity-like object is an AND group.
@@ -1946,11 +2071,5 @@ declare class NoProductCatalogForCartError extends Error {
1946
2071
  declare class NoShoppingListForCartError extends Error {
1947
2072
  constructor();
1948
2073
  }
1949
- declare class NoTabAsIndentError extends Error {
1950
- constructor();
1951
- }
1952
- declare class BadIndentationError extends Error {
1953
- constructor();
1954
- }
1955
2074
 
1956
- export { type AddedIngredient, type AddedRecipe, type AddedRecipeOptions, type AlternativeIngredientRef, type AndGroup, type ArbitraryScalable, type ArbitraryScalableItem, BadIndentationError, type CartContent, type CartMatch, type CartMisMatch, type CategorizedIngredients, type Category, CategoryConfig, type CategoryIngredient, type Cookware, type CookwareFlag, type CookwareItem, type DecimalValue, type FixedNumericValue, type FixedValue, type FlatAndGroup, type FlatGroup, type FlatOrGroup, type FractionValue, type GetIngredientQuantitiesOptions, type Group, type Ingredient, type IngredientAlternative, type IngredientAlternativeBase, type IngredientExtras, type IngredientFlag, type IngredientItem, type IngredientQuantityAndGroup, type IngredientQuantityGroup, type MaybeNestedAndGroup, type MaybeNestedGroup, type MaybeNestedOrGroup, type MaybeScalableQuantity, type Metadata, type MetadataObject, type MetadataScalingVar, type MetadataSource, type MetadataTime, type MetadataValue, NoProductCatalogForCartError, type NoProductMatchErrorCode, NoShoppingListForCartError, NoTabAsIndentError, type Note, type NoteItem, type OrGroup, Pantry, type PantryItem, type PantryItemToml, type PantryOptions, ProductCatalog, type ProductMatch, type ProductMisMatch, type ProductOption, type ProductOptionBase, type ProductOptionCore, type ProductSelection, type ProductSize, type QuantityBase, type QuantityWithExtendedUnit, type QuantityWithPlainUnit, type QuantityWithUnitDef, type QuantityWithUnitLike, type Range, type RawQuantityGroup, Recipe, type RecipeAlternatives, type RecipeChoices, type RecipeWithFactor, type RecipeWithServings, Section, ShoppingCart, type ShoppingCartOptions, type ShoppingCartSummary, ShoppingList, type SpecificUnitSystem, type Step, type StepItem, type TextAttribute, type TextItem, type TextValue, type Timer, type TimerItem, type ToBaseBySystem, type Unit, type UnitDefinition, type UnitDefinitionLike, type UnitFractionConfig, type UnitSystem, type UnitType, type WithOptionalQuantity, convertQuantityToSystem, formatExtendedQuantity, formatItemQuantity, formatNumericValue, formatQuantity, formatQuantityWithUnit, formatSingleValue, formatUnit, hasAlternatives, isAlternativeSelected, isAndGroup, isGroupedItem, isSimpleGroup, renderFractionAsVulgar };
2075
+ export { type AddedIngredient, type AddedRecipe, type AddedRecipeOptions, type AlternativeIngredientRef, type AndGroup, type ArbitraryScalable, type ArbitraryScalableItem, type CartContent, type CartMatch, type CartMisMatch, type CategorizedIngredients, type Category, CategoryConfig, type CategoryIngredient, type Cookware, type CookwareFlag, type CookwareItem, type DecimalValue, type FixedNumericValue, type FixedValue, type FlatAndGroup, type FlatGroup, type FlatOrGroup, type FractionValue, type GetIngredientQuantitiesOptions, type Group, type Ingredient, type IngredientAlternative, type IngredientAlternativeBase, type IngredientExtras, type IngredientFlag, type IngredientItem, type IngredientQuantityAndGroup, type IngredientQuantityGroup, type MaybeNestedAndGroup, type MaybeNestedGroup, type MaybeNestedOrGroup, type MaybeScalableQuantity, type Metadata, type MetadataObject, type MetadataSource, type MetadataTime, type MetadataValue, NoProductCatalogForCartError, type NoProductMatchErrorCode, NoShoppingListForCartError, type Note, type NoteItem, type OrGroup, Pantry, type PantryItem, type PantryItemToml, type PantryOptions, ProductCatalog, type ProductMatch, type ProductMisMatch, type ProductOption, type ProductOptionBase, type ProductOptionCore, type ProductSelection, type ProductSize, type QuantityBase, type QuantityWithExtendedUnit, type QuantityWithPlainUnit, type QuantityWithUnitDef, type QuantityWithUnitLike, type Range, type RawQuantityGroup, Recipe, type RecipeAlternatives, type RecipeChoices, type RecipeWithFactor, type RecipeWithServings, Section, ShoppingCart, type ShoppingCartOptions, type ShoppingCartSummary, ShoppingList, type SpecificUnitSystem, type Step, type StepItem, type TextAttribute, type TextItem, type TextValue, type Timer, type TimerItem, type ToBaseBySystem, type Unit, type UnitDefinition, type UnitDefinitionLike, type UnitFractionConfig, type UnitSystem, type UnitType, type WithOptionalQuantity, type Yield, convertQuantityToSystem, formatExtendedQuantity, formatItemQuantity, formatNumericValue, formatQuantity, formatQuantityWithUnit, formatSingleValue, formatUnit, getEffectiveChoices, hasAlternatives, isAlternativeSelected, isAndGroup, isGroupedItem, isSectionActive, isSimpleGroup, isStepActive, renderFractionAsVulgar };