Calio/shared/nutrition-trends.js
Elijah 7288e56c02
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 32s
Polish PWA gestures, transitions and nutrition averages
2026-10-22 09:16:52 -07:00

39 lines
1.3 KiB
JavaScript

import { emptyNutrients, totalNutrients } from "./domain.js";
// The cutoff is a logging preference, not a nutritional recommendation.
// A fixed kcal cutoff remains predictable even if a calorie goal is disabled.
/**
* @param {import("../src/types").Entry[]} entries
* @param {string[]} dates
* @param {number} minimumCalories
*/
export function nutritionSummary(entries, dates, minimumCalories) {
/** @type {Map<string, import("../src/types").Entry[]>} */
const grouped = new Map();
for (const entry of entries) {
const group = grouped.get(entry.date) || [];
group.push(entry);
grouped.set(entry.date, group);
}
const days = dates.map((date) => {
const logs = grouped.get(date) || [];
const nutrients = totalNutrients(logs);
return {
date,
logged: logs.length > 0,
included: logs.length > 0 && nutrients.calories >= minimumCalories,
nutrients,
};
});
const included = days.filter((day) => day.included);
const average = emptyNutrients();
for (const day of included)
for (const key of Object.keys(average))
average[key] += day.nutrients[key] / included.length;
return {
days,
average,
includedCount: included.length,
excludedCount: days.filter((day) => day.logged && !day.included).length,
};
}