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

@tmlmt/cooklang-parser / ShoppingCart

Class: ShoppingCart

Shopping Cart Manager: a tool to find the best combination of products to buy (defined in a ProductCatalog) to satisfy a ShoppingList.

Example

ts
const shoppingList = new ShoppingList();
const recipe = new Recipe("@flour{600%g}");
shoppingList.addRecipe(recipe);

const catalog = new ProductCatalog();
catalog.products = [
  {
    id: "flour-1kg",
    productName: "Flour (1kg)",
    ingredientName: "flour",
    price: 10,
    size: { type: "fixed", value: { type: "decimal", value: 1000 } },
    unit: "g",
  },
  {
    id: "flour-500g",
    productName: "Flour (500g)",
    ingredientName: "flour",
    price: 6,
    size: { type: "fixed", value: { type: "decimal", value: 500 } },
    unit: "g",
  },
];

const shoppingCart = new ShoppingCart({list: shoppingList, catalog}))
shoppingCart.buildCart();

Constructors

Constructor

new ShoppingCart(options?): ShoppingCart

Creates a new ShoppingCart instance

Parameters

options?

ShoppingCartOptions

Options for the constructor

Returns

ShoppingCart

Properties

cart

cart: CartContent = []

The content of the cart


match

match: CartMatch = []

The ingredients that were successfully matched with products


misMatch

misMatch: CartMisMatch = []

The ingredients that could not be matched with products


productCatalog?

optional productCatalog: ProductCatalog

The product catalog to use for matching products


shoppingList?

optional shoppingList: ShoppingList

The shopping list to build the cart from


summary

summary: ShoppingCartSummary

Key information about the shopping cart

Methods

buildCart()

buildCart(): boolean

Builds the cart from the shopping list and product catalog

Returns

boolean

true if all ingredients in the shopping list have been matched to products in the catalog, or false otherwise

Remarks

  • If a combination of product(s) is successfully found for a given ingredient, the latter will be listed in the match array in addition to that combination being added to the cart.
  • Otherwise, the latter will be listed in the misMatch array. Possible causes can be:
    • No product is listed in the catalog for that ingredient
    • The ingredient has no quantity, a text quantity
    • The ingredient's quantity unit is incompatible with the units of the candidate products listed in the catalog

Throws

NoProductCatalogForCartError if no product catalog is set

Throws

NoShoppingListForCartError if no shopping list is set


setProductCatalog()

setProductCatalog(catalog): void

Sets the product catalog to use for matching products To use if a catalog was not provided at the creation of the instance

Parameters

catalog

ProductCatalog

The ProductCatalog to set

Returns

void


setShoppingList()

setShoppingList(list): void

Sets the shopping list to build the cart from. To use if a shopping list was not provided at the creation of the instance

Parameters

list

ShoppingList

The ShoppingList to set

Returns

void


summarize()

summarize(): ShoppingCartSummary

Calculate the cart's key info and store it in the cart's summary property. This function is automatically invoked by buildCart() method.

Returns

ShoppingCartSummary

the total price and number of items in the cart