@tmlmt/cooklang-parser / Recipe
Class: Recipe
Recipe parser.
Usage
You can either directly provide the recipe string when creating the instance e.g. const recipe = new Recipe('Add @eggs{3}'), or create it first and then pass the recipe string to the parse() method.
Look at the properties to see how the recipe's properties are parsed.
Example
import { Recipe } from "@tmlmt/cooklang-parser";
const recipeString = `
---
title: Pancakes
tags: [breakfast, easy]
---
Crack the @eggs{3} with @flour{100%g} and @milk{200%mL}
Melt some @butter{50%g} in a #pan on medium heat.
Cook for ~{5%minutes} on each side.
`
const recipe = new Recipe(recipeString);Constructors
Constructor
new Recipe(
content?):Recipe
Creates a new Recipe instance.
Parameters
content?
string
The recipe content to parse.
Returns
Recipe
Properties
choices
choices:
RecipeAlternatives
The default or manual choice of alternative ingredients. Contains the full context including alternatives list and active selection index.
cookware
cookware:
Cookware[] =[]
The parsed recipe cookware.
ingredients
ingredients:
Ingredient[] =[]
The parsed recipe ingredients.
metadata
metadata:
Metadata={}
The parsed recipe metadata.
sections
sections:
Section[] =[]
The parsed recipe sections.
servings?
optionalservings:number
The parsed recipe servings. Used for scaling. Parsed from one of Metadata.servings, Metadata.yield or Metadata.serves metadata fields.
See
scaleBy() and scaleTo() methods
timers
timers:
Timer[] =[]
The parsed recipe timers.
Methods
calc_ingredient_quantities()
calc_ingredient_quantities(
choices?):ComputedIngredient[]
Calculates ingredient quantities based on the provided choices. Returns a list of computed ingredients with their total quantities.
Parameters
choices?
The recipe choices to apply when computing quantities. If not provided, uses the default choices (first alternative for each item).
Returns
An array of ComputedIngredient with quantityTotal calculated based on choices.
clone()
clone():
Recipe
Clones the recipe.
Returns
Recipe
A new Recipe instance with the same properties.
parse()
parse(
content):void
Parses a recipe from a string.
Parameters
content
string
The recipe content to parse.
Returns
void
scaleBy()
scaleBy(
factor):Recipe
Scales the recipe by a factor.
Parameters
factor
The factor to scale the recipe by. While integers can be passed as-is, it is recommended to pass fractions as Big values, e.g. Big(num).div(den) in order to avoid undesirable floating point operation inaccuracies.
number | Big
Returns
Recipe
A new Recipe instance with the scaled ingredients.
scaleTo()
scaleTo(
newServings):Recipe
Scales the recipe to a new number of servings. In practice, it calls scaleBy with a factor corresponding to the ratio between newServings and the recipe's servings value.
Parameters
newServings
number
The new number of servings.
Returns
Recipe
A new Recipe instance with the scaled ingredients.
Throws
Error if the recipe does not contains an initial servings value