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

@tmlmt/cooklang-parser / ProductCatalog

Class: ProductCatalog ​

Product Catalog Manager: used in conjunction with ShoppingCart

Usage ​

You can either directly populate the products by feeding the products property. Alternatively, you can provide a catalog in TOML format to either the constructor itself or to the parse() method.

Catalog format ​

Product catalogs are TOML tables grouped by ingredient name. Each ingredient table can contain:

  • an optional aliases array with alternate ingredient names,
  • product entries whose keys are any string except the reserved aliases key,
    • each product entry must define name, size, and price. size can be a single string such as "1" or "100%g", or an array of equivalent sizes such as ['1%dozen', '12'].
    • arbitrary metadata on each product entry, which is preserved when parsing.

See the product catalog guide for the full syntax and examples.

Example ​

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

const catalog = `
[eggs]
aliases = ["oeuf", "huevo"]
01123 = { name = "Single Egg", size = "1", price = 2 }
11244 = { name = "Pack of 6 eggs", size = "6", price = 10 }

[flour]
aliases = ["farine", "Mehl"]
01124 = { name = "Small pack", size = "100%g", price = 1.5 }
14141 = { name = "Big pack", size = "6%kg", price = 10 }
`
const catalog = new ProductCatalog(catalog);
const eggs = catalog.products.find(p => p.ingredientName === "eggs");

Constructors ​

Constructor ​

new ProductCatalog(tomlContent?): ProductCatalog

Parameters ​

tomlContent? ​

string

Returns ​

ProductCatalog

Properties ​

products ​

products: ProductOption[] = []

Methods ​

add() ​

add(productOption): void

Adds a product to the catalog.

Parameters ​

productOption ​

ProductOption

The product to add.

Returns ​

void


parse() ​

parse(tomlContent): ProductOption[]

Parses a TOML string into a list of product options.

Parameters ​

tomlContent ​

string

The TOML string to parse.

Returns ​

ProductOption[]

A parsed list of ProductOption.


remove() ​

remove(productId): void

Removes a product from the catalog by its ID.

Parameters ​

productId ​

string

The ID of the product to remove.

Returns ​

void


stringify() ​

stringify(): string

Stringifies the catalog to a TOML string.

Returns ​

string

The TOML string representation of the catalog.