⚠️ This is pre-release documentation for v3. For stable docs, visit v2.
Skip to content

@tmlmt/cooklang-parser / isAlternativeSelected

Function: isAlternativeSelected() ​

isAlternativeSelected(recipe, choices, item, alternativeIndex?): boolean

Determines if a specific alternative in an IngredientItem is selected based on the applied choices.

Use this in renderers to determine how an ingredient alternative should be displayed.

Parameters ​

recipe ​

Recipe

The Recipe instance containing choices

choices ​

RecipeChoices

The choices that have been made

item ​

IngredientItem

The IngredientItem to check

alternativeIndex? ​

number

The index within item.alternatives to check (for inline alternatives only)

Returns ​

boolean

true if this alternative is the selected one

Example ​

typescript
const recipe = new Recipe(cooklangText);
for (const item of step.items) {
  if (item.type === 'ingredient') {
    item.alternatives.forEach((alt, idx) => {
      const isSelected = isAlternativeSelected(recipe, choices, item, idx);
      // Render differently based on isSelected
    });
  }
}