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 add_recipe())
  • 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.set_category_config(categoryConfig);
// Quantities are automatically calculated and ingredients categorized
// when adding a recipe
shoppingList.add_recipe(recipe1);

Constructors

Constructor

new ShoppingList(category_config_str?): ShoppingList

Creates a new ShoppingList instance

Parameters

category_config_str?

The category configuration to parse.

string | CategoryConfig

Returns

ShoppingList

Properties

categories?

optional categories: CategorizedIngredients

The categorized ingredients in the shopping list.


category_config?

optional category_config: CategoryConfig

The category configuration for the shopping list.


ingredients

ingredients: Ingredient[] = []

The ingredients in the shopping list.


recipes

recipes: AddedRecipe[] = []

The recipes in the shopping list.

Methods

add_recipe()

add_recipe(recipe, factor): void

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

Parameters

recipe

Recipe

The recipe to add.

factor

number = 1

The factor to scale the recipe by.

Returns

void


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


remove_recipe()

remove_recipe(index): void

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

Parameters

index

number

The index of the recipe to remove.

Returns

void


set_category_config()

set_category_config(config): void

Sets the category configuration for the shopping list and automatically categorize current ingredients from the list.

Parameters

config

The category configuration to parse.

string | CategoryConfig

Returns

void