export function groupsToItems(groups) { return groups.map((group) => group.shoppingListItems).flat() } export function uniqueMeals(shoppingListItems) { const mealsWithDuplicates = shoppingListItems.map((item) => item.meal).filter((m) => m) const mealsLookup = mealsWithDuplicates.reduce((acc, meal) => { acc[meal.id] ??= meal return acc }, {}) return Object.values(mealsLookup) } export function itemsToGroups(shoppingListItems) { const ingredients_by_product_id = {} const ingredients_by_name = {} for (const item of shoppingListItems) { if (item.ingredient.product) { let group = ingredients_by_product_id[item.ingredient.product.id] if (!group) { group = ingredients_by_product_id[item.ingredient.product.id] = { product: item.ingredient.product, shoppingListItems: [], } } group.shoppingListItems.push(item) } else { let group = ingredients_by_name[item.ingredient.name] if (!group) { group = ingredients_by_name[item.ingredient.name] = { name: item.ingredient.name, shoppingListItems: [], } } group.shoppingListItems.push(item) } } return [...Object.values(ingredients_by_product_id), ...Object.values(ingredients_by_name)] }