Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
units: a unit switch keeps the bar a bar — 45 lb becomes the 20 kg bar, not 20.5 kg
Boris Kurktchievd28d9dd3bf70
diff --git a/frontend/src/lib/units.js b/frontend/src/lib/units.jsindex 41ae4d052..165176466 100644--- a/frontend/src/lib/units.js+++ b/frontend/src/lib/units.js@@ -4,6 +4,8 @@// and back lands where it started for any plate-loadable number.import { isSideSet, syncSideAggregate } from './workout-model.js'import { workoutVolume } from './history.js'+import { EXIDX } from './exercises.js'+import { defaultBarWeight } from './bar.js'const LB_PER_KG = 2.2046226218@@ -42,6 +44,24 @@ const convTarget = (cfg, from, to) => {if (Array.isArray(out.warmup)) out.warmup = out.warmup.map(w => (w && w.weight != null ? { ...w, weight: convertWeight(w.weight, from, to) } : w))return out}+// A bar is a stamped object, not a number: the 45 lb bar IS the 20 kg bar (44.1 lb), so an+// override that equals the old unit's default for that bar type drops out and the new unit's+// default takes over — 45 lb → 20 kg, not 20.5, which with a kg plate set would leave every row+// "1 kg short". An explicit 0 ("no bar", lib/bar.js) stays 0. A custom bar converts like any+// other weight (a 33 lb women's bar → 15 kg), and drops out too if it lands on the new default.+const convBarWeights = (bw, from, to) => {+ const out = {}+ for (const [id, v] of Object.entries(bw || {})) {+ if (typeof v !== 'number' || !Number.isFinite(v) || v < 0) continue+ if (v === 0) { out[id] = 0; continue }+ const eq = EXIDX[id]?.eq+ if (eq && v === defaultBarWeight(eq, from)) continue+ const c = convertWeight(v, from, to)+ if (eq && c === defaultBarWeight(eq, to)) continue+ out[id] = c+ }+ return out+}const convEntry = (e, from, to) => {if (!e || typeof e !== 'object') return ereturn {@@ -72,7 +92,7 @@ export function convertStateUnit(S, to) {if (Array.isArray(S.bodyweight)) out.bodyweight = S.bodyweight.map(b => ({ ...b, w: bw(b.w) }))if (S.targetW != null) out.targetW = bw(S.targetW)if (S.exWeights) out.exWeights = Object.fromEntries(Object.entries(S.exWeights).map(([k, v]) => [k, v && typeof v === 'object' ? { ...v, w: c(v.w) } : c(v)]))- if (S.barWeights) out.barWeights = Object.fromEntries(Object.entries(S.barWeights).map(([k, v]) => [k, c(v)]))+ if (S.barWeights) out.barWeights = convBarWeights(S.barWeights, from, to)if (Array.isArray(S.routines)) out.routines = S.routines.map(r => ({ ...r, ex: (r.ex || []).map(cfg => convTarget(cfg, from, to)) }))if (Array.isArray(S.workouts)) out.workouts = S.workouts.map(convSession)if (S.active) out.active = convSession(S.active)diff --git a/frontend/src/lib/units.test.js b/frontend/src/lib/units.test.jsindex 6066d37bd..c54985c74 100644--- a/frontend/src/lib/units.test.js+++ b/frontend/src/lib/units.test.js@@ -24,7 +24,7 @@ describe('convertWeight', () => {describe('convertStateUnit', () => {const S = {unit: 'kg', targetW: 80, bodyweight: [{ d: '2026-01-01', w: 82.4, t: 1 }],- exWeights: { '0025': { w: 80, d: '2026-01-01' }, legacy: 100 }, barWeights: { '0025': 20 },+ exWeights: { '0025': { w: 80, d: '2026-01-01' }, legacy: 100 }, barWeights: { '0025': 15 },routines: [{ id: 'r', ex: [{ id: '0025', sets: 3, reps: 5, weight: 80, inc: 2.5, warmup: [{ weight: 40, reps: 8 }] }, { id: 'plank', mode: 'time', sec: 30, inc: 5 }] }],workouts: [{ id: 'w', entries: [{ id: '0025', topW: 80, target: { weight: 80 }, sets: [{ w: 80, r: 5, done: true, drops: [{ w: 60, r: 5 }] }] }] }],active: { id: 'a', entries: [{ id: '0025', sets: [{ w: 82.5, r: 5, done: false }] }] },@@ -37,7 +37,7 @@ describe('convertStateUnit', () => {expect(out.bodyweight[0]).toEqual({ d: '2026-01-01', w: 181.7, t: 1 })expect(out.exWeights['0025'].w).toBe(176.5)expect(out.exWeights.legacy).toBe(220.5)- expect(out.barWeights['0025']).toBe(44)+ expect(out.barWeights['0025']).toBe(33) // a custom 15 kg bar is the 33 lb barexpect(out.routines[0].ex[0]).toMatchObject({ weight: 176.5, inc: 5.5, warmup: [{ weight: 88, reps: 8 }] })expect(out.routines[0].ex[1]).toEqual({ id: 'plank', mode: 'time', sec: 30, inc: 5 }) // seconds stay secondsexpect(out.workouts[0].entries[0]).toMatchObject({ topW: 176.5, target: { weight: 176.5 } })@@ -83,3 +83,33 @@ describe('convertStateUnit', () => {expect(back.active.bw).toBe(78.6)})})++describe('convertStateUnit: bar weights', () => {+ // 0025 barbell bench press (barbell), 1344 an EZ-bar exercise, 1383 a sled (not a bar).+ const conv = (barWeights, from, to) => convertStateUnit({ unit: from, barWeights }, to).barWeights++ it('an override equal to the old default drops out, so the new default applies (45 lb IS the 20 kg bar)', () => {+ expect(conv({ '0025': 45 }, 'lb', 'kg')).toEqual({})+ expect(conv({ '0025': 20 }, 'kg', 'lb')).toEqual({})+ expect(conv({ 1344: 25 }, 'lb', 'kg')).toEqual({}) // EZ bar 25 lb ↔ 10 kg+ expect(conv({ 1344: 10 }, 'kg', 'lb')).toEqual({})+ })++ it('an explicit 0 ("no bar") stays 0', () => {+ expect(conv({ '0025': 0 }, 'lb', 'kg')).toEqual({ '0025': 0 })+ expect(conv({ '0025': 0 }, 'kg', 'lb')).toEqual({ '0025': 0 })+ })++ it('a custom bar converts like any weight, and drops out when it lands on the new default', () => {+ expect(conv({ '0025': 33 }, 'lb', 'kg')).toEqual({ '0025': 15 }) // women's bar+ expect(conv({ '0025': 15 }, 'kg', 'lb')).toEqual({ '0025': 33 })+ expect(conv({ '0025': 44 }, 'lb', 'kg')).toEqual({}) // 44 lb → 20 kg = the kg default+ expect(conv({ 1383: 100 }, 'lb', 'kg')).toEqual({ 1383: 45.25 }) // a sled's own weight is not a bar+ expect(conv({ 'no-such-id': 45 }, 'lb', 'kg')).toEqual({ 'no-such-id': 20.5 })+ })++ it('junk in the map is dropped', () => {+ expect(conv({ '0025': '45', 1344: -1, 1383: null }, 'lb', 'kg')).toEqual({})+ })+})+