31 lines
937 B
TypeScript
31 lines
937 B
TypeScript
import type { Gender, Metrics, StandardUnit } from "@blade-and-brawn/domain";
|
|
import avgWeightDataJson from "./data/avg-weights.json" with { type: "json" };
|
|
|
|
export const getAvgWeight = (gender: Gender, age: number) => {
|
|
type AvgWeightData = {
|
|
metadata: {
|
|
unit: StandardUnit;
|
|
};
|
|
weights: Metrics[];
|
|
};
|
|
const avgWeightData = avgWeightDataJson as AvgWeightData;
|
|
|
|
const avgWeights = avgWeightData.weights.filter((a) => a.gender === gender);
|
|
const firstAvgWeight = avgWeights[0];
|
|
if (!firstAvgWeight) throw new Error("No average weights");
|
|
|
|
const closest = {
|
|
diff: Math.abs(firstAvgWeight.age - age),
|
|
avg: firstAvgWeight,
|
|
};
|
|
for (const avg of avgWeights) {
|
|
const diff = Math.abs(avg.age - age);
|
|
if (diff < closest.diff) {
|
|
closest.diff = diff;
|
|
closest.avg = avg;
|
|
}
|
|
}
|
|
|
|
return closest.avg.weight;
|
|
};
|