2025-07-28 23:22:58 +00:00
|
|
|
export function groupsToItems(groups) {
|
|
|
|
|
return groups.map(group => group.shoppingListItems).flat();
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-30 08:10:22 +00:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:22:58 +00:00
|
|
|
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: []
|
|
|
|
|
};
|
2024-10-13 08:19:36 +00:00
|
|
|
}
|
2025-07-28 23:22:58 +00:00
|
|
|
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: []
|
|
|
|
|
};
|
2024-10-13 08:19:36 +00:00
|
|
|
}
|
2025-07-28 23:22:58 +00:00
|
|
|
group.shoppingListItems.push(item);
|
2024-05-19 10:21:49 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-28 23:22:58 +00:00
|
|
|
return [
|
|
|
|
|
...Object.values(ingredients_by_product_id),
|
|
|
|
|
...Object.values(ingredients_by_name)
|
|
|
|
|
]
|
2024-05-12 03:32:18 +00:00
|
|
|
}
|