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

@tmlmt/cooklang-parser / ShoppingList

Class: ShoppingList

Shopping List generator.

Usage

  • Create a new ShoppingList instance with an optional category configuration (see constructor)
  • Add recipes, scaling them as needed (see addRecipe())
  • Categorize the ingredients (see categorize())

Example

typescript
import * as fs from "fs";
import { ShoppingList } from @tmlmt/cooklang-parser;

const categoryConfig = fs.readFileSync("./myconfig.txt", "utf-8")
const recipe1 = new Recipe(fs.readFileSync("./myrecipe.cook", "utf-8"));
const shoppingList = new ShoppingList();
shoppingList.setCategoryConfig(categoryConfig);
// Quantities are automatically calculated and ingredients categorized
// when adding a recipe
shoppingList.addRecipe(recipe1);

Constructors

Constructor

new ShoppingList(categoryConfigStr?): ShoppingList

Creates a new ShoppingList instance

Parameters

categoryConfigStr?

The category configuration to parse.

string | CategoryConfig

Returns

ShoppingList

Properties

categories?

optional categories: CategorizedIngredients

The categorized ingredients in the shopping list.


categoryConfig?

optional categoryConfig: CategoryConfig

The category configuration for the shopping list.


ingredients

ingredients: AddedIngredient[] = []

The ingredients in the shopping list.


pantry?

optional pantry: Pantry

The original pantry (never mutated by recipe calculations).


recipes

recipes: AddedRecipe[] = []

The recipes in the shopping list.


unitSystem?

optional unitSystem: SpecificUnitSystem

The unit system to use for quantity simplification. When set, overrides per-recipe unit systems.

Methods

addPantry()

addPantry(pantry, options?): void

Adds a pantry to the shopping list. On-hand pantry quantities will be subtracted from recipe ingredient needs on each recalculation.

Parameters

pantry

A Pantry instance or a TOML string to parse.

string | Pantry

options?

PantryOptions

Options for pantry parsing (only used when providing a TOML string).

Returns

void


addRecipe()

addRecipe(recipe, options?): void

Adds a recipe to the shopping list, then automatically recalculates the quantities and recategorize the ingredients.

Parameters

recipe

Recipe

The recipe to add.

options?

AddedRecipeOptions = {}

Options for adding the recipe.

Returns

void

Throws

Error if the recipe has alternatives without corresponding choices.


categorize()

categorize(): void

Categorizes the ingredients in the shopping list Will use the category config if any, otherwise all ingredients will be placed in the "other" category

Returns

void


getPantry()

getPantry(): Pantry | undefined

Returns the resulting pantry with quantities updated to reflect what was consumed by the shopping list's recipes. Returns undefined if no pantry was added.

Returns

Pantry | undefined

The resulting Pantry, or undefined.


removeRecipe()

removeRecipe(index): void

Removes a recipe from the shopping list, then automatically recalculates the quantities and recategorize the ingredients.

Parameters

index

number

The index of the recipe to remove.

Returns

void


setCategoryConfig()

setCategoryConfig(config): void

Sets the category configuration for the shopping list and automatically categorize current ingredients from the list. Also propagates the configuration to the pantry if one is set.

Parameters

config

The category configuration to parse.

string | CategoryConfig

Returns

void