From a9b18051f73275bb9ef4660fcb13f791eb02d565 Mon Sep 17 00:00:00 2001 From: jableader Date: Sun, 19 May 2024 21:57:28 +1000 Subject: [PATCH] Better unit showing on shopping list --- src/components/shopping/ShoppingListItem.vue | 18 +++++++++-- src/components/shopping/shopping.js | 33 ++++++++++++++++---- src/units.js | 1 + 3 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/components/shopping/ShoppingListItem.vue b/src/components/shopping/ShoppingListItem.vue index 85c5da5..bcabd2d 100644 --- a/src/components/shopping/ShoppingListItem.vue +++ b/src/components/shopping/ShoppingListItem.vue @@ -9,7 +9,7 @@ , - {{ total.quantity }} {{ total.unit }} + {{ formatQuantity(total.quantity) }} {{ total.unit }} ✓ {{ getFriendlyDate(foundDate) }} ? {{ getFriendlyDate(foundDate) }} @@ -19,10 +19,10 @@ , and - {{ source.ingredient.quantity }} {{ source.ingredient.unit }} for {{ source.person.name }} + {{ formatQuantity(source.ingredient.quantity) }} {{ source.ingredient.unit }} for {{ source.person.name }} - {{ source.ingredient.quantity }} {{ source.ingredient.unit }} + {{ formatQuantity(source.ingredient.quantity) }} {{ source.ingredient.unit }} in {{ source.recipe.name }} for {{ source.meal.meal_date.toLocaleDateString("en-AU", { weekday: 'long', month: 'long', day: 'numeric' }) }} @@ -134,6 +134,18 @@ export default { } return "moments ago"; + }, + formatQuantity(quantity) { + const log10 = Math.log10(quantity); + if (log10 < 0) { + return quantity.toPrecision(2); + } + else if (log10 < 1) { + return quantity.toFixed(1); + } + else { + return quantity.toFixed(0); + } } } } diff --git a/src/components/shopping/shopping.js b/src/components/shopping/shopping.js index 37a91b6..e2de8e8 100644 --- a/src/components/shopping/shopping.js +++ b/src/components/shopping/shopping.js @@ -52,16 +52,37 @@ export function groupingToIngredients(grouping) { } function calculateTotals(sources) { - const totals = {}; + let byBaseUnit = {}; for (const { quantity, unit } of sources) { const conversion = units.getConversionFactor(unit) ?? { unit, factor: 1 }; - - const quantityInBase = quantity / conversion.factor; - if (!totals[conversion.unit]) { - totals[conversion.unit] = quantityInBase; + + if (!byBaseUnit[conversion.unit]) { + byBaseUnit[conversion.unit] = {}; + } + + if (!byBaseUnit[conversion.unit][unit]) { + byBaseUnit[conversion.unit][unit] = { quantity, factor: conversion.factor }; } else { - totals[conversion.unit] += quantityInBase; + byBaseUnit[conversion.unit][unit].quantity += quantity; + } + } + + const totals = {}; + for (const baseUnit in byBaseUnit) { + if (Object.keys(byBaseUnit[baseUnit]).length === 1) { + const unit = Object.keys(byBaseUnit[baseUnit])[0]; + const { quantity, } = byBaseUnit[baseUnit][unit]; + totals[unit] = quantity; + } + else { + let total = 0; + for (const unit in byBaseUnit[baseUnit]) { + const { quantity, factor } = byBaseUnit[baseUnit][unit]; + total += quantity / factor; + } + + totals[baseUnit] = total; } } diff --git a/src/units.js b/src/units.js index cc979ad..2075942 100644 --- a/src/units.js +++ b/src/units.js @@ -16,6 +16,7 @@ export const equivalentUnits = { 'litres': { 'l': 1, 'liter': 1, + 'litre': 1, 'ml': 1000, 'milliliters': 1000,