Move domain models into domain package with schemas and validate with zod + elysia. Some maintenence as well
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import type { Gender, Metrics, StandardUnit } from "@blade-and-brawn/domain";
|
||||
import avgWeightDataJson from "./data/avg-weights.json" assert { 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;
|
||||
};
|
||||
Reference in New Issue
Block a user