Estimated 1RM: the calculation, and only from sets that can support it (#18)

Working out how strong a set was meant leaving the app. Epley by default, with Brzycki
and Lombardi available and documented.

Two things it deliberately refuses to do:
 · guess above 12 reps, where the formulas disagree by double digits and the answer says
   more about work capacity than about maximal strength;
 · read `topW`, the working weight you confirm after an exercise — it carries no rep
   count, so it cannot produce an estimate at all.

It also knows nothing about the exercise database. An estimate needs a weight AND reps,
and only reps-mode sets carry both, so cardio and timed sets drop out of every scan on
their own rather than through a type check that would need keeping in sync.
This commit is contained in:
Duarte Santos committed 2026-07-25 14:58:10 +02:00
1 parent 0818f1b32b
commit a54bad2641
2 files changed
+230

No files matched your search

+82
View File
@@ -0,0 +1,82 @@
// Estimated one-rep max (issue #18).
//
// Deliberately knows nothing about the exercise database: an estimate needs a weight AND a
// rep count, and only reps-mode sets carry both. Cardio sets ({min, speed}) and timed sets
// ({sec, w}) therefore drop out of every scan here on their own — there is no exercise-type
// check to keep in sync.
//
// Formulas are the usual submaximal-load estimators. Epley is the default because it is the
// one most lifters have seen; all of them agree closely at low reps and diverge as reps rise,
// which is exactly why REP_CAP exists.
// Above this many reps an estimate says more about work capacity than about maximal strength,
// and the formulas disagree by double digits. Refusing to guess beats printing a fantasy.
export const REP_CAP = 12
export const FORMULAS = {
// Epley 1985 — w · (1 + r/30)
epley: (w, r) => w * (1 + r / 30),
// Brzycki 1993 — w · 36/(37 − r); undefined at r ≥ 37, but REP_CAP is far below that
brzycki: (w, r) => w * 36 / (37 - r),
// Lombardi 1989 — w · r^0.10
lombardi: (w, r) => w * Math.pow(r, 0.1)
}
export const DEFAULT_FORMULA = 'epley'
// Estimate a 1RM from one set. Returns null for anything it cannot honestly answer:
// missing/zero/negative load, no reps, non-finite input, or more reps than REP_CAP.
// A single rep is not an estimate — it is the measurement — and comes back unchanged.
export function estimate1RM(w, r, formula = DEFAULT_FORMULA) {
const weight = Number(w)
const reps = Number(r)
if (!isFinite(weight) || !isFinite(reps)) return null
if (weight <= 0 || reps < 1) return null
if (reps > REP_CAP) return null
const fn = FORMULAS[formula] || FORMULAS[DEFAULT_FORMULA]
const est = reps === 1 ? weight : fn(weight, Math.round(reps))
if (!isFinite(est) || est <= 0) return null
return Math.round(est * 10) / 10
}
// Best estimate out of one workout entry's completed sets.
// `topW` is ignored on purpose: it records the working weight a user confirmed after the
// exercise, with no rep count attached, so it cannot produce an estimate.
export function bestSetOf(entry, formula = DEFAULT_FORMULA) {
let best = null
;(entry?.sets || []).forEach(s => {
if (!s.done) return
const est = estimate1RM(s.w, s.r, formula)
if (est !== null && (!best || est > best.est)) best = { est, w: Number(s.w), r: Math.round(Number(s.r)) }
})
return best
}
// One point per workout in which the exercise produced an estimate — feeds the trend chart.
// Chronological, matching the order workouts are appended in.
export function e1rmSeries(S, exId, formula = DEFAULT_FORMULA) {
const pts = []
;(S.workouts || []).forEach(w => {
const entry = w.entries.find(e => e.id === exId)
if (!entry) return
const best = bestSetOf(entry, formula)
if (best) pts.push({ t: w.start, d: w.d, y: best.est, w: best.w, r: best.r })
})
return pts
}
// All-time best estimate for an exercise, with the set and date it came from — the source
// matters, because "142.5 kg est. from 100×10" is a very different claim from "from 140×1".
export function best1RM(S, exId, formula = DEFAULT_FORMULA) {
let best = null
e1rmSeries(S, exId, formula).forEach(p => { if (!best || p.y > best.est) best = { est: p.y, w: p.w, r: p.r, d: p.d, t: p.t } })
return best
}
// Did this workout beat every estimate that came before it? Used for the finish summary,
// so it compares against history that does not yet contain `w`.
export function is1RMRecord(S, exId, entry, formula = DEFAULT_FORMULA) {
const now = bestSetOf(entry, formula)
if (!now) return null
const prev = best1RM(S, exId, formula)
return !prev || now.est > prev.est ? { ...now, prev: prev ? prev.est : 0 } : null
}
+148
View File
@@ -0,0 +1,148 @@
import { describe, it, expect } from 'vitest'
import { estimate1RM, bestSetOf, e1rmSeries, best1RM, is1RMRecord, REP_CAP, FORMULAS } from './onerm.js'
describe('estimate1RM', () => {
it('returns the load unchanged for a single rep', () => {
expect(estimate1RM(100, 1)).toBe(100)
expect(estimate1RM(62.5, 1)).toBe(62.5)
})
it('matches Epley by hand across the usual rep ranges', () => {
expect(estimate1RM(100, 5)).toBe(116.7) // 100 · (1 + 5/30)
expect(estimate1RM(100, 10)).toBe(133.3)
expect(estimate1RM(80, 8)).toBe(101.3)
expect(estimate1RM(60, 3)).toBe(66)
})
it('rounds to one decimal', () => {
expect(estimate1RM(101.25, 7)).toBe(124.9)
expect(Number.isInteger(estimate1RM(100, 3) * 10)).toBe(true)
})
it('refuses reps beyond the cap rather than guessing', () => {
expect(estimate1RM(100, REP_CAP)).not.toBeNull()
expect(estimate1RM(100, REP_CAP + 1)).toBeNull()
expect(estimate1RM(60, 30)).toBeNull()
})
it('rejects invalid, zero and negative input', () => {
expect(estimate1RM(0, 5)).toBeNull()
expect(estimate1RM(-100, 5)).toBeNull()
expect(estimate1RM(100, 0)).toBeNull()
expect(estimate1RM(100, -3)).toBeNull()
expect(estimate1RM(undefined, 5)).toBeNull()
expect(estimate1RM(100, undefined)).toBeNull()
expect(estimate1RM(NaN, 5)).toBeNull()
expect(estimate1RM(Infinity, 5)).toBeNull()
expect(estimate1RM('', '')).toBeNull()
})
it('accepts numeric strings, the shape number inputs arrive in', () => {
expect(estimate1RM('100', '5')).toBe(116.7)
})
it('offers the other documented formulas and agrees with them at low reps', () => {
expect(estimate1RM(100, 5, 'brzycki')).toBe(112.5)
expect(estimate1RM(100, 5, 'lombardi')).toBe(117.5)
expect(Object.keys(FORMULAS)).toContain('epley')
})
it('keeps the formulas within a few percent up to 8 reps, and diverges most at the cap', () => {
const spread = r => Math.max(...Object.keys(FORMULAS).map(f => estimate1RM(100, r, f)))
- Math.min(...Object.keys(FORMULAS).map(f => estimate1RM(100, r, f)))
expect(spread(1)).toBe(0) // one rep is measured, not estimated
for (let r = 2; r <= 8; r++) expect(spread(r)).toBeLessThan(6)
const upTo = []
for (let r = 1; r < REP_CAP; r++) upTo.push(spread(r))
expect(spread(REP_CAP)).toBeGreaterThan(Math.max(...upTo)) // why REP_CAP exists
})
it('falls back to the default for an unknown formula name', () => {
expect(estimate1RM(100, 5, 'nope')).toBe(estimate1RM(100, 5))
})
})
describe('bestSetOf', () => {
it('picks the highest estimate, not the heaviest set', () => {
const entry = { id: 'x', sets: [
{ w: 100, r: 5, done: true }, // 116.7
{ w: 110, r: 3, done: true }, // 121.0
{ w: 120, r: 1, done: true } // 120.0
] }
expect(bestSetOf(entry)).toEqual({ est: 121, w: 110, r: 3 })
})
it('ignores sets that were never checked off', () => {
const entry = { id: 'x', sets: [{ w: 100, r: 5, done: true }, { w: 200, r: 5, done: false }] }
expect(bestSetOf(entry).w).toBe(100)
})
it('ignores topW, which carries no rep count', () => {
const entry = { id: 'x', topW: 200, sets: [{ w: 100, r: 5, done: true }] }
expect(bestSetOf(entry).est).toBe(116.7)
})
it('returns null for cardio and timed entries', () => {
expect(bestSetOf({ id: 'c', sets: [{ min: 20, speed: 9, done: true }] })).toBeNull()
expect(bestSetOf({ id: 'p', sets: [{ sec: 60, w: 0, done: true }] })).toBeNull()
expect(bestSetOf({ id: 'p', sets: [{ sec: 60, w: 20, done: true }] })).toBeNull()
})
it('survives a missing or empty entry', () => {
expect(bestSetOf(null)).toBeNull()
expect(bestSetOf({ sets: [] })).toBeNull()
})
})
const S = {
workouts: [
{ d: '2026-01-01', start: 1, entries: [{ id: 'bench', sets: [{ w: 80, r: 5, done: true }] }] },
{ d: '2026-01-08', start: 2, entries: [{ id: 'squat', sets: [{ w: 100, r: 5, done: true }] }] },
{ d: '2026-01-15', start: 3, entries: [{ id: 'bench', sets: [{ w: 90, r: 5, done: true }, { w: 90, r: 3, done: false }] }] },
{ d: '2026-01-22', start: 4, entries: [{ id: 'bench', sets: [{ w: 85, r: 5, done: true }] }] },
{ d: '2026-01-29', start: 5, entries: [{ id: 'run', sets: [{ min: 30, speed: 10, done: true }] }] }
]
}
describe('e1rmSeries / best1RM', () => {
it('yields one chronological point per workout that produced an estimate', () => {
const pts = e1rmSeries(S, 'bench')
expect(pts.map(p => p.d)).toEqual(['2026-01-01', '2026-01-15', '2026-01-22'])
expect(pts.map(p => p.y)).toEqual([93.3, 105, 99.2])
})
it('reports the all-time best with the set behind it', () => {
expect(best1RM(S, 'bench')).toEqual({ est: 105, w: 90, r: 5, d: '2026-01-15', t: 3 })
})
it('has nothing to say about cardio or an unknown exercise', () => {
expect(e1rmSeries(S, 'run')).toEqual([])
expect(best1RM(S, 'run')).toBeNull()
expect(best1RM(S, 'nope')).toBeNull()
expect(best1RM({ workouts: [] }, 'bench')).toBeNull()
expect(best1RM({}, 'bench')).toBeNull()
})
})
describe('is1RMRecord', () => {
it('flags a session that beats every previous estimate', () => {
const rec = is1RMRecord(S, 'bench', { id: 'bench', sets: [{ w: 95, r: 5, done: true }] })
expect(rec).toEqual({ est: 110.8, w: 95, r: 5, prev: 105 })
})
it('stays quiet when the session does not beat the record', () => {
expect(is1RMRecord(S, 'bench', { id: 'bench', sets: [{ w: 90, r: 5, done: true }] })).toBeNull()
expect(is1RMRecord(S, 'bench', { id: 'bench', sets: [{ w: 80, r: 5, done: true }] })).toBeNull()
})
it('counts the first ever estimate as a record', () => {
const rec = is1RMRecord(S, 'deadlift', { id: 'deadlift', sets: [{ w: 140, r: 3, done: true }] })
expect(rec.prev).toBe(0)
expect(rec.est).toBe(154)
})
it('says nothing for a timed or unfinished entry', () => {
expect(is1RMRecord(S, 'plank', { id: 'plank', sets: [{ sec: 90, done: true }] })).toBeNull()
expect(is1RMRecord(S, 'bench', { id: 'bench', sets: [{ w: 200, r: 5, done: false }] })).toBeNull()
})
})