show < sign instead of rounding to 0

This commit is contained in:
Timon Ringwald 2022-09-09 17:44:41 +02:00
parent 1d0ad7f222
commit 710080fb89

64
main.js
View File

@ -114,7 +114,7 @@ async function addMenuItem(food) {
nodeRoot.setAttribute("data-food-name", food.name); nodeRoot.setAttribute("data-food-name", food.name);
itemName.textContent = food.name; itemName.textContent = food.name;
itemKcal.textContent = `${nutritions100.kcal} kcal/100${food.unit}`; itemKcal.textContent = `${formatValue(nutritions100.kcal)} kcal/100${food.unit}`;
itemUnit.textContent = food.unit; itemUnit.textContent = food.unit;
fatAmount.textContent = `${food.fat}g`; fatAmount.textContent = `${food.fat}g`;
@ -163,22 +163,36 @@ async function addMenuItem(food) {
function updateFoodAmount(food, amount, kcalAmount, fatAmount, carbsAmount, sugarAmount, proteinsAmount, alcAmount) { function updateFoodAmount(food, amount, kcalAmount, fatAmount, carbsAmount, sugarAmount, proteinsAmount, alcAmount) {
const {fat, carbs, sugar, proteins, alc, kcal} = calculateKcal(food, amount); const {fat, carbs, sugar, proteins, alc, kcal} = calculateKcal(food, amount);
kcalAmount.textContent = `${parseInt(kcal)}kcal`; setAmount(kcalAmount, kcal, "kcal");
fatAmount.textContent = `${parseInt(fat)}g`; setAmount(fatAmount, fat, "g");
carbsAmount.textContent = `${parseInt(carbs)}g`; setAmount(carbsAmount, carbs, "g");
sugarAmount.textContent = `${parseInt(sugar)}g`; setAmount(sugarAmount, sugar, "g");
proteinsAmount.textContent = `${parseInt(proteins)}g`; setAmount(proteinsAmount, proteins, "g");
alcAmount.textContent = `${parseInt(alc)}g`; setAmount(alcAmount, alc, "g");
updateSummary(); updateSummary();
} }
function setAmount(element, amount, unit) {
element.textContent = `${formatValue(amount)}${unit}`;
element.setAttribute("data-value", amount);
}
function formatValue(value) {
if (value > 0 && value < 1) {
return "<1"
}
return Math.round(value);
}
function calculateKcal(food, amount) { function calculateKcal(food, amount) {
const fat = Math.round(food.fat / 100 * amount) const fat = food.fat / 100 * amount;
const carbs = Math.round(food.carbs / 100 * amount); const carbs = food.carbs / 100 * amount;
const sugar = Math.round(food.sugar / 100 * amount); const sugar = food.sugar / 100 * amount;
const proteins = Math.round(food.proteins / 100 * amount); const proteins = food.proteins / 100 * amount;
const alc = Math.round(amount * food.alc / 100); const alc = amount * food.alc / 100;
const kcal = Math.round(fat * 9 + carbs * 4 + proteins * 4 + alc * 7); const kcal = fat * 9 + carbs * 4 + proteins * 4 + alc * 7;
const score = kcal / amount; const score = kcal / amount;
return {fat, carbs, sugar, proteins, alc, kcal, score} return {fat, carbs, sugar, proteins, alc, kcal, score}
} }
@ -195,20 +209,20 @@ function updateSummary() {
let alc = 0; let alc = 0;
for (let menuItem of menuItems) { for (let menuItem of menuItems) {
energy += parseInt(menuItem.querySelector(".menu-item-energy").textContent); energy += parseFloat(menuItem.querySelector(".menu-item-energy").getAttribute("data-value"));
fat += parseInt(menuItem.querySelector(".menu-item-fat").textContent); fat += parseFloat(menuItem.querySelector(".menu-item-fat").getAttribute("data-value"));
carbs += parseInt(menuItem.querySelector(".menu-item-carbs").textContent); carbs += parseFloat(menuItem.querySelector(".menu-item-carbs").getAttribute("data-value"));
sugar += parseInt(menuItem.querySelector(".menu-item-sugar").textContent); sugar += parseFloat(menuItem.querySelector(".menu-item-sugar").getAttribute("data-value"));
proteins += parseInt(menuItem.querySelector(".menu-item-proteins").textContent); proteins += parseFloat(menuItem.querySelector(".menu-item-proteins").getAttribute("data-value"));
alc += parseInt(menuItem.querySelector(".menu-item-alc").textContent); alc += parseFloat(menuItem.querySelector(".menu-item-alc").getAttribute("data-value"));
} }
summaryEnergy.textContent = `${energy}kcal`; summaryEnergy.textContent = `${formatValue(energy)}kcal`;
summaryFat.textContent = `${fat}g`; summaryFat.textContent = `${formatValue(fat)}g`;
summaryCarbs.textContent = `${carbs}g`; summaryCarbs.textContent = `${formatValue(carbs)}g`;
summarySugar.textContent = `${sugar}g`; summarySugar.textContent = `${formatValue(sugar)}g`;
summaryProteins.textContent = `${proteins}g`; summaryProteins.textContent = `${formatValue(proteins)}g`;
summaryAlc.textContent = `${alc}g`; summaryAlc.textContent = `${formatValue(alc)}g`;
} }
function getScoreColor(density, unit) { function getScoreColor(density, unit) {