Polish PWA gestures, transitions and nutrition averages
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 32s
All checks were successful
Verify and publish Calio container / build-and-push (push) Successful in 32s
This commit is contained in:
parent
b19de6f9ea
commit
7288e56c02
16 changed files with 1019 additions and 245 deletions
39
shared/nutrition-trends.js
Normal file
39
shared/nutrition-trends.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
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,
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue