Skip to content

Commit 1d2d5e5

Browse files
committed
fix: add alternative units to ingredients list
1 parent 43404a7 commit 1d2d5e5

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

docs/guide-extensions.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,55 @@ When referencing ingredients, the added quantities will be converted to [Decimal
9696
```
9797

9898
Also works with Cookware and Timers
99-
99+
100100
## Cookware quantities
101101

102102
- Cookware can also be quantified (without any unit, e.g. `#bowls{2}`)
103103
- Quantities will be added similarly as ingredients if cookware is referenced, e.g. `#&bowls{2}`
104104

105+
## Alternative units
106+
107+
You can define equivalent quantities in different units for the same ingredient using the pipe `|` separator within the curly braces.
108+
109+
Usage: `@flour{100%g|3.5%oz}`
110+
111+
This is useful for providing multiple units (e.g. metric and imperial) simultaneously for the same ingredient. The first unit is considered the primary unit.
112+
113+
## Ingredient alternatives
114+
115+
### Inline alternatives
116+
117+
You can specify alternative ingredients directly in the ingredient syntax using pipes: `@baseName{quantity}|altName{altQuantity}[note]|...`
118+
119+
This allows users to select from multiple alternatives when computing recipe quantities.
120+
121+
Use cases:
122+
- `@milk{200%ml}|almond milk{100%ml}[vegan version]|soy milk{150%ml}[another vegan option]`
123+
- `@sugar{100%g}|brown sugar{100%g}[for a richer flavor]`
124+
125+
When inline alternatives are defined, the recipe's [`choices`](/api/interfaces/RecipeChoices) property will be populated. You can then use the `calc_ingredient_quantities()` method to compute quantities corresponding to the user's choices.
126+
127+
All modifiers (`&`, `-`, `?`) work with inline alternatives:
128+
`@&milk{200%ml}|-almond milk{100%ml}[vegan version]|?soy milk{150%ml}[another vegan option]`
129+
130+
### Grouped alternatives
131+
132+
Ingredients can also have alternatives by grouping them with the same group key using the syntax: `@|groupKey|ingredientName{}`
133+
134+
This is useful when you want to provide alternative choices in your recipe text naturally:
135+
136+
Use cases:
137+
- `Add @|milk|milk{200%ml} or @|milk|almond milk{100%ml} or @|milk|oat milk{150%ml} for a vegan version`
138+
- `Add some @|spices|salt{} or maybe some @|spices|pepper{}`
139+
140+
When grouped alternatives are defined, the recipe's [`choices`](/api/interfaces/RecipeChoices) property will be populated with available alternatives for each group. You can then use the `calc_ingredient_quantities()` method to compute quantities corresponding to the user's choices.
141+
142+
All modifiers (`&`, `-`, `?`) work with grouped alternatives:
143+
```
144+
Add @|flour|&flour tipo 00{100%g} or @|flour|flour tipo 1{50%g}
145+
Add @|spices|-salt{} or @|spices|?pepper{}
146+
```
147+
105148
## Ingredient aliases
106149

107150
- `@ingredientName|displayAlias{}` will add the ingredient as "ingredientName" in the ingredients list, but will display is as "displayAlias" in the preparation step.

src/classes/recipe.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,13 @@ export class Recipe {
598598
mainQuantity,
599599
) as QuantityWithPlainUnit;
600600

601+
// Store additional equivalents if there are any
602+
if (alternative.quantity.equivalents.length > 1) {
603+
plainQuantity.equivalents = alternative.quantity.equivalents
604+
.slice(1)
605+
.map((eq) => toPlainUnit(eq) as QuantityWithPlainUnit);
606+
}
607+
601608
// Check if this ingredient item has alternatives (inline or grouped)
602609
const hasInlineAlternatives = item.alternatives.length > 1;
603610
const hasGroupedAlternatives =

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,8 @@ export interface QuantityBase {
777777
*/
778778
export interface QuantityWithPlainUnit extends QuantityBase {
779779
unit?: string;
780+
/** Optional equivalent quantities in different units (for alternative units like `@flour{100%g|3.5%oz}`) */
781+
equivalents?: QuantityWithPlainUnit[];
780782
}
781783

782784
/**

test/recipe_parsing.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,6 +1739,22 @@ Another step.
17391739
value: { type: "decimal", decimal: 1 },
17401740
},
17411741
unit: "bag",
1742+
equivalents: [
1743+
{
1744+
quantity: {
1745+
type: "fixed",
1746+
value: { type: "decimal", decimal: 0.22 },
1747+
},
1748+
unit: "lb",
1749+
},
1750+
{
1751+
quantity: {
1752+
type: "fixed",
1753+
value: { type: "decimal", decimal: 3.5 },
1754+
},
1755+
unit: "oz",
1756+
},
1757+
],
17421758
},
17431759
],
17441760
usedAsPrimary: true,

0 commit comments

Comments
 (0)