@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
The Recipe instance containing choices
choices
The choices that have been made
item
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(item, idx, recipe, choices);
// Render differently based on isSelected
});
}
}