Skip to content

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.

fix(import): convert Apple Health weights using each record's unit

AC0cdee00e6484
diff --git a/frontend/src/lib/import-bodyweight.test.js b/frontend/src/lib/import-bodyweight.test.js
new file mode 100644
index 000000000..10f853431
--- /dev/null
+++ b/frontend/src/lib/import-bodyweight.test.js
@@ -0,0 +1,27 @@
+import { describe, expect, it } from 'vitest'
+import { parseBodyweight } from './import-csv.js'
+
+const record = (day, value, unit) => `<Record type="HKQuantityTypeIdentifierBodyMass" unit="${unit}" value="${value}" startDate="2026-09-${day} 08:00:00 +0000"/>`
+
+describe('Apple Health body-weight units', () => {
+ it.each([
+ ['kg', [80, 81.6]],
+ ['lb', [176.4, 180]],
+ ])('converts each record into %s independently', (unit, expected) => {
+ const parsed = parseBodyweight(record('01', 80, 'kg') + record('02', 180, 'lb'), { unit })
+ expect(parsed.bodyweight.map(b => b.w)).toEqual(expected)
+ expect(parsed.converted).toBe(true)
+ expect(parsed.fileUnit).toBe('kg / lb')
+ })
+
+ it('does not let record order change another day’s weight', () => {
+ const kg = record('01', 80, 'kg'), lb = record('02', 180, 'lb')
+ expect(parseBodyweight(kg + lb).bodyweight).toEqual(parseBodyweight(lb + kg).bodyweight)
+ })
+
+ it('preserves conversion for single-unit XML and CSV exports', () => {
+ expect(parseBodyweight(record('01', 180, 'lb')).bodyweight[0].w).toBe(81.6)
+ expect(parseBodyweight('Date,Weight (lbs)\n2026-09-01,180').bodyweight[0].w).toBe(81.6)
+ expect(parseBodyweight(record('01', 80, 'kg')).converted).toBe(false)
+ })
+})
diff --git a/frontend/src/lib/import-csv.js b/frontend/src/lib/import-csv.js
index 9315b920d..a7ff3c272 100644
--- a/frontend/src/lib/import-csv.js
+++ b/frontend/src/lib/import-csv.js
@@ -615,8 +615,7 @@ export function parseBodyweight(text, { unit = 'kg' } = {}) {
if (!val || !dt) continue
const when = parseWhen(dt[1])
if (!when) continue
- if (u) fileUnit = /lb/i.test(u[1]) ? 'lb' : 'kg'
- out.set(when.d, { w: parseFloat(val[1]), t: new Date(dt[1]).getTime() || null })
+ out.set(when.d, { w: parseFloat(val[1]), unit: u && /lb/i.test(u[1]) ? 'lb' : 'kg', t: new Date(dt[1]).getTime() || null })
}
} else {
const rows = parseCSV(s)
@@ -637,14 +636,20 @@ export function parseBodyweight(text, { unit = 'kg' } = {}) {
}
if (!out.size) return { error: 'unrecognised' }
- const converted = !!fileUnit && fileUnit !== unit
- const conv = converted
- ? (fileUnit === 'lb' ? x => Math.round(x * LB_TO_KG * 10) / 10 : x => Math.round(x / LB_TO_KG * 10) / 10)
- : x => Math.round(x * 10) / 10
+ const units = new Set([...out.values()].map(b => b.unit || fileUnit).filter(Boolean))
+ fileUnit = [...units].sort().join(' / ')
+ const converted = [...units].some(sourceUnit => sourceUnit !== unit)
+ const conv = b => {
+ const sourceUnit = b.unit || fileUnit
+ const w = sourceUnit && sourceUnit !== unit
+ ? (sourceUnit === 'lb' ? b.w * LB_TO_KG : b.w / LB_TO_KG)
+ : b.w
+ return Math.round(w * 10) / 10
+ }
const dates = [...out.keys()].sort()
return {
kind: 'bodyweight', source: 'Apple Health',
- bodyweight: dates.map(d => ({ d, w: conv(out.get(d).w), t: out.get(d).t || new Date(d).getTime() })),
+ bodyweight: dates.map(d => ({ d, w: conv(out.get(d)), t: out.get(d).t || new Date(d).getTime() })),
fileUnit, converted, from: dates[0], to: dates[dates.length - 1],
}
}