munch-ease-frontend/src/components/shopping/shopping.js

45 lines
1.5 KiB
JavaScript
Raw Normal View History

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);
}
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);
2024-05-19 10:21:49 +00:00
}
}
return [
...Object.values(ingredients_by_product_id),
...Object.values(ingredients_by_name)
]
2024-05-12 03:32:18 +00:00
}