@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.
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.find("oeuf");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.