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

@tmlmt/cooklang-parser / Pantry

Class: Pantry ​

Pantry Inventory Manager: parses and queries a pantry inventory file.

Usage ​

Create a new Pantry instance with optional TOML content and options (see constructor), then query items using getDepletedItems(), getExpiredItems(), isLow(), or isExpired().

A Pantry can also be attached to a ShoppingList via addPantry() so that on-hand stock is subtracted from recipe ingredient needs.

Example ​

typescript
import { Pantry } from "@tmlmt/cooklang-parser";

const pantryToml = `
[fridge]
milk = { expire = "10.05.2024", quantity = "1%L" }

[freezer]
spinach = { quantity = "1%kg", low = "200%g" }
`;

const pantry = new Pantry(pantryToml);
console.log(pantry.getExpiredItems());
console.log(pantry.isLow("spinach"));

See ​

Pantry Configuration section of the cooklang specs

Constructors ​

Constructor ​

new Pantry(tomlContent?, options?): Pantry

Creates a new Pantry instance.

Parameters ​

tomlContent? ​

string

Optional TOML content to parse.

options? ​

PantryOptions = {}

Optional configuration options.

Returns ​

Pantry

Properties ​

items ​

items: PantryItem[] = []

The parsed pantry items.

Methods ​

findItem() ​

findItem(name): PantryItem | undefined

Finds a pantry item by name, using exact match first, then alias lookup via the stored CategoryConfig.

Parameters ​

name ​

string

The name to search for.

Returns ​

PantryItem | undefined

The matching pantry item, or undefined if not found.


getDepletedItems() ​

getDepletedItems(): PantryItem[]

Returns all items that are depleted (quantity = 0) or below their low threshold.

Returns ​

PantryItem[]

An array of depleted pantry items.


getExpiredItems() ​

getExpiredItems(nbDays?): PantryItem[]

Returns all items whose expiration date is within nbDays days from today (or already passed).

Parameters ​

nbDays? ​

number = 0

Number of days ahead to check. Defaults to 0 (already expired).

Returns ​

PantryItem[]

An array of expired pantry items.


isExpired() ​

isExpired(itemName, nbDays?): boolean

Checks if a specific item is expired or expires within nbDays days.

Parameters ​

itemName ​

string

The name of the item to check (supports aliases if CategoryConfig is set).

nbDays? ​

number = 0

Number of days ahead to check. Defaults to 0.

Returns ​

boolean

true if the item is expired, false otherwise. Returns false if item not found.


isLow() ​

isLow(itemName): boolean

Checks if a specific item is low (quantity = 0 or below low threshold).

Parameters ​

itemName ​

string

The name of the item to check (supports aliases if CategoryConfig is set).

Returns ​

boolean

true if the item is low, false otherwise. Returns false if item not found.


parse() ​

parse(tomlContent): PantryItem[]

Parses a TOML string into pantry items.

Parameters ​

tomlContent ​

string

The TOML string to parse.

Returns ​

PantryItem[]

The parsed list of pantry items.


setCategoryConfig() ​

setCategoryConfig(config): void

Sets a category configuration for alias-based item lookups.

Parameters ​

config ​

CategoryConfig

The category configuration to use.

Returns ​

void