143 lines
4.6 KiB
JavaScript
143 lines
4.6 KiB
JavaScript
'use strict';
|
|
|
|
/**
|
|
* Energy maths for the tracker.
|
|
*
|
|
* Internal units are metric (kg, cm). The app stores weight in kg and height in cm,
|
|
* but exposes pounds/lbs and feet+inches in the API and UI, and accepts lbs/week as the
|
|
* goal rate.
|
|
*
|
|
* Burn model (an estimate, not a lab measurement):
|
|
* basal = BMR (Mifflin-St Jeor) x 1.2 for sedentary daily living
|
|
* steps = steps x body weight (kg) x 0.0005 (~0.04 kcal/step at 80 kg)
|
|
* workout= MET x 3.5 x body weight (kg) / 200 x minutes
|
|
* Intake target = basal + steps + workouts +/- the goal rate (3500 kcal per lb).
|
|
*/
|
|
|
|
const LB_PER_KG = 2.20462;
|
|
const KG_PER_LB = 1 / LB_PER_KG;
|
|
const IN_PER_CM = 1 / 2.54;
|
|
const CM_PER_IN = 2.54;
|
|
const IN_PER_FT = 12;
|
|
|
|
const KCAL_PER_LB = 3500;
|
|
const KCAL_PER_KG = KCAL_PER_LB * LB_PER_KG; // 7716.16...
|
|
const SEDENTARY_FACTOR = 1.2;
|
|
|
|
function clamp(n, min, max) {
|
|
return Math.min(Math.max(n, min), max);
|
|
}
|
|
|
|
/** Convert imperial height (feet, inches) and weight (pounds) to metric. */
|
|
function imperialToMetric({ feet = 0, inches = 0, pounds = 0 }) {
|
|
const totalInches = Math.max(0, Number(feet) * IN_PER_FT + Number(inches));
|
|
const cm = totalInches * CM_PER_IN;
|
|
const kg = Number(pounds) * KG_PER_LB;
|
|
return { heightCm: cm, weightKg: kg };
|
|
}
|
|
|
|
/** Convert metric back to imperial for display. */
|
|
function metricToImperial({ weightKg, heightCm }) {
|
|
const pounds = Math.round(Number(weightKg) * LB_PER_KG * 10) / 10;
|
|
const totalInches = Math.round(Number(heightCm) / CM_PER_IN);
|
|
const feet = Math.floor(totalInches / IN_PER_FT);
|
|
const inches = totalInches % IN_PER_FT;
|
|
return { pounds, feet, inches, totalInches };
|
|
}
|
|
|
|
/** Round a pounds/week value to the nearest 0.5 lb. */
|
|
function roundLbWeek(n) {
|
|
return Math.round(n * 2) / 2;
|
|
}
|
|
|
|
function bmrKcal({ sex, weightKg, heightCm, age }) {
|
|
const base = 10 * weightKg + 6.25 * heightCm - 5 * age;
|
|
return Math.round(Math.max(0, base + (sex === 'female' ? -161 : 5)));
|
|
}
|
|
|
|
function kcalFromSteps(steps, weightKg) {
|
|
return Math.round(clamp(Number(steps) || 0, 0, 200000) * (Number(weightKg) || 0) * 0.0005);
|
|
}
|
|
|
|
function kcalFromExercise({ met, minutes, weightKg }) {
|
|
const m = clamp(Number(met) || 0, 0, 30);
|
|
const mins = clamp(Number(minutes) || 0, 0, 1440);
|
|
return Math.round((m * 3.5 * (Number(weightKg) || 0) / 200) * mins);
|
|
}
|
|
|
|
function dailyTargets(profile, burn = {}) {
|
|
const weight = Number(profile.weight_kg) || 75;
|
|
const height = Number(profile.height_cm) || 175;
|
|
const age = Number(profile.age) || 30;
|
|
const sex = profile.sex === 'female' ? 'female' : 'male';
|
|
|
|
const bmr = bmrKcal({ sex, weightKg: weight, heightCm: height, age });
|
|
const basal = Math.round(bmr * SEDENTARY_FACTOR);
|
|
const steps = clamp(Number(burn.steps) || 0, 0, 200000);
|
|
const stepKcal = kcalFromSteps(steps, weight);
|
|
const exerciseKcal = Math.round(clamp(Number(burn.exerciseKcal) || 0, 0, 10000));
|
|
const totalBurn = basal + stepKcal + exerciseKcal;
|
|
|
|
const goal = ['lose', 'maintain', 'gain'].includes(profile.goal) ? profile.goal : 'maintain';
|
|
// Accept lbs/week (new) or kg/week (legacy). Internal deficit math uses kcal per kg.
|
|
const rateLbWeek = Number(profile.rate_lb_week);
|
|
const rateKgWeek = Number(profile.rate_kg_week);
|
|
let rateKg = null;
|
|
if (Number.isFinite(rateLbWeek) && rateLbWeek > 0) {
|
|
rateKg = rateLbWeek * KG_PER_LB;
|
|
} else if (Number.isFinite(rateKgWeek) && rateKgWeek > 0) {
|
|
rateKg = rateKgWeek;
|
|
}
|
|
rateKg = clamp(rateKg, 0, 1.5);
|
|
const adjust =
|
|
goal === 'lose' ? -Math.round(rateKg * KCAL_PER_KG / 7)
|
|
: goal === 'gain' ? Math.round(rateKg * KCAL_PER_KG / 7)
|
|
: 0;
|
|
|
|
const floor = sex === 'female' ? 1200 : 1500;
|
|
const targetKcal = Math.max(floor, Math.round(totalBurn + adjust));
|
|
|
|
const proteinTarget = Number(profile.protein_target_g)
|
|
|| Math.round(weight * (goal === 'lose' ? 2.0 : 1.6));
|
|
const fatTarget = Math.round((targetKcal * 0.3) / 9);
|
|
const carbsTarget = Math.max(
|
|
0,
|
|
Math.round((targetKcal - proteinTarget * 4 - fatTarget * 9) / 4)
|
|
);
|
|
|
|
const outRateLbWeek = rateKg > 0
|
|
? Math.round((rateKg / KG_PER_LB) * 2) / 2
|
|
: Math.round(rateKgWeek * 2) / 2;
|
|
return {
|
|
bmrKcal: bmr,
|
|
basalKcal: basal,
|
|
stepKcal,
|
|
exerciseKcal,
|
|
totalBurnKcal: totalBurn,
|
|
adjustKcal: adjust,
|
|
targetKcal,
|
|
rateLbWeek: outRateLbWeek,
|
|
protein_g: Math.round(proteinTarget),
|
|
carbs_g: carbsTarget,
|
|
fat_g: fatTarget,
|
|
};
|
|
}
|
|
|
|
module.exports = {
|
|
LB_PER_KG,
|
|
KG_PER_LB,
|
|
CM_PER_IN,
|
|
IN_PER_CM,
|
|
IN_PER_FT,
|
|
KCAL_PER_LB,
|
|
KCAL_PER_KG,
|
|
SEDENTARY_FACTOR,
|
|
imperialToMetric,
|
|
metricToImperial,
|
|
roundLbWeek,
|
|
bmrKcal,
|
|
kcalFromSteps,
|
|
kcalFromExercise,
|
|
dailyTargets,
|
|
};
|