@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
aliasesarray with alternate ingredient names, - product entries whose keys are any string except the reserved
aliaseskey,- each product entry must define
name,size, andprice.sizecan 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.
- each product entry must define
See the product catalog guide for the full syntax and examples.
Example
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
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
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.