UNPKG

wilks

Version:

Calculate wilks score, dots, one rep max and more. Wilks formula and Dots are primarily used in powerlifting contests to identify the best lifter across different weight classes.

66 lines 2.66 kB
import { coefficients } from "./constants.js"; export function roundToDecimal(input, decimal) { const factor = Math.pow(10, decimal); return Math.round(input * factor) / factor; } export function convertWeight(weight, unit, decimal = 1) { return roundToDecimal(unit === "lb" ? weight * 0.45359237 : weight, decimal); } export function getTotal(input) { if ("total" in input) return input.total; return Object.values(input).reduce((a, b) => a + b, 0); } ; export function calculateCoefficient(input, formula) { const { gender, bodyweight, unit } = input; const coefficient = coefficients[formula][gender]; const bw = convertWeight(bodyweight, unit); return coefficient.reduce((acc, curr, idx) => { return idx === 0 ? acc + curr : acc + (curr * Math.pow(bw, idx)); }, 0); } export function validateInput(input) { const { bodyweight, gender, unit, total, squat, bench, deadlift } = input; const errors = []; const isValidNumber = (value) => typeof value === "number" && value > 0; const isValidGender = (g) => g === "male" || g === "female"; const isValidUnit = (u) => u === "kg" || u === "lb"; if (!isValidNumber(bodyweight)) { errors.push("Bodyweight must be a number greater than 0."); } if (!isValidGender(gender)) { errors.push("Gender must be either 'male' or 'female'."); } if (!isValidUnit(unit)) { errors.push("Unit must be either 'kg' or 'lb'."); } const hasTotal = typeof total === "number"; const hasLifts = squat !== undefined || bench !== undefined || deadlift !== undefined; if (hasTotal && hasLifts) { errors.push("Use either total or squat/bench/deadlift, not both."); } if (hasTotal && !isValidNumber(total)) { errors.push("Invalid total. Use a number greater than 0."); } if (!hasTotal) { if (!isValidNumber(squat)) errors.push("Invalid squat. Use a number greater than 0."); if (!isValidNumber(bench)) errors.push("Invalid bench. Use a number greater than 0."); if (!isValidNumber(deadlift)) errors.push("Invalid deadlift. Use a number greater than 0."); } if (errors.length) { throw new TypeError(errors.join(" ")); } } export function validateCompetition(competition) { const validCompetitions = ["clpl", "clbp", "eqpl", "eqbp"]; if (typeof competition !== "string" || !validCompetitions.includes(competition)) { throw new TypeError("Please enter valid competition (\"clpl\", \"clbp\", \"eqpl\", \"eqbp\")"); } } //# sourceMappingURL=helpers.js.map