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.

format: tests for fmtDateRange, durPart, capWords, fmtNum; fmtNum no longer prints -0

Duarte Santosacd321ef6319
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f4102a919..b0f0c6eb9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -80,6 +80,7 @@ you leave it alone. Handy for deciding whether those legs are really ready (than
- Jumping jacks, burpees and the other cardio moves can be counted in sets x reps (or timed):
their settings now offer Cardio, Reps and Time, Cardio still the default (#678).
- Add exercise → By muscle has the Favourites chip too, for the muscle you picked (#669).
+- A weight change too small to show (say 0.03 kg) reads 0 instead of a lonely "-0".
- Focus mode stays on the screen with lots of sets or a long exercise name (#655).
- Chart dates under the graph say each day once over a short span, and months get their year
once a chart covers more than twelve of them (thanks @lodepav).
diff --git a/frontend/src/lib/format.helpers.test.js b/frontend/src/lib/format.helpers.test.js
new file mode 100644
index 000000000..33471850d
--- /dev/null
+++ b/frontend/src/lib/format.helpers.test.js
@@ -0,0 +1,73 @@
+// The small formatting helpers most screens lean on and no other test pins down: a span of days,
+// a duration that may be unknown, names cased for a toast, volume and plain numbers.
+import { afterEach, describe, expect, it } from 'vitest'
+import { fmtDateRange, durPart, capWords, fmtVol, fmtNum, setWeightDecimals } from './format.js'
+
+const NOW = new Date('2026-10-19T12:00:00')
+
+describe('fmtDateRange', () => {
+ it('leaves the year out for a span inside the current year', () => {
+ expect(fmtDateRange('2026-02-03', '2026-12-21', false, NOW)).toBe('3 Feb – 21 Dec')
+ })
+ it('shows the year on both ends when the span crosses one', () => {
+ expect(fmtDateRange('2025-12-30', '2026-01-02', false, NOW)).toBe('30 Dec 2025 – 2 Jan 2026')
+ })
+ it('shows the year for a span that lies in another year', () => {
+ expect(fmtDateRange('2025-03-01', '2025-03-04', false, NOW)).toBe('1 Mar 2025 – 4 Mar 2025')
+ })
+ it('is a single date when both ends are the same day or there is no end', () => {
+ expect(fmtDateRange('2025-03-01', '2025-03-01', true, NOW)).toBe('Sat, 1 Mar 2025')
+ expect(fmtDateRange('2026-03-01', null, false, NOW)).toBe('1 Mar')
+ })
+ it('is empty with no start', () => {
+ expect(fmtDateRange('', '2026-03-01', false, NOW)).toBe('')
+ expect(fmtDateRange(null, null, false, NOW)).toBe('')
+ })
+})
+
+describe('durPart', () => {
+ it('leaves an unknown or sub-minute duration out instead of saying "0 min"', () => {
+ expect(durPart(0)).toEqual([])
+ expect(durPart(59999)).toEqual([])
+ expect(durPart(undefined)).toEqual([])
+ })
+ it('formats a known one', () => {
+ expect(durPart(60000)).toEqual(['1 min'])
+ expect(durPart((62 * 60 + 5) * 1000)).toEqual(['1h 2m'])
+ })
+})
+
+describe('capWords', () => {
+ it('capitalises every word, also after brackets, hyphens and slashes', () => {
+ expect(capWords('barbell bench press (wide-grip)/close')).toBe('Barbell Bench Press (Wide-Grip)/Close')
+ })
+ it('handles letters outside ASCII and leaves upper case alone', () => {
+ expect(capWords('über kreuz')).toBe('Über Kreuz')
+ expect(capWords('EZ bar curl')).toBe('EZ Bar Curl')
+ })
+ it('reads nothing as an empty string', () => {
+ expect(capWords(null)).toBe('')
+ expect(capWords(undefined)).toBe('')
+ })
+})
+
+describe('fmtNum / fmtVol', () => {
+ afterEach(() => setWeightDecimals(1))
+
+ it('rounds to the profile\'s decimals and groups thousands', () => {
+ expect(fmtVol(7535.5, 'kg')).toBe('7,535.5 kg')
+ expect(fmtNum(62.5)).toBe('62.5')
+ expect(fmtNum(80)).toBe('80')
+ setWeightDecimals(2)
+ expect(fmtNum(21.25)).toBe('21.25')
+ })
+
+ // A change that rounds away ("-0.03 kg") printed "-0": a minus sign in front of nothing.
+ it('never prints a negative zero', () => {
+ expect(fmtNum(-0.03)).toBe('0')
+ expect(fmtNum(-0)).toBe('0')
+ setWeightDecimals(2)
+ expect(fmtNum(-0.004)).toBe('0')
+ expect(fmtNum(-0.05)).toBe('-0.05')
+ })
+})
diff --git a/frontend/src/lib/format.js b/frontend/src/lib/format.js
index caec1c61e..10406a322 100644
--- a/frontend/src/lib/format.js
+++ b/frontend/src/lib/format.js
@@ -72,7 +72,8 @@ export const setWeightDecimals = n => { decimals = n === 2 ? 2 : 1 }
export const weightDecimals = () => decimals
export const fmtNum = n => {
const p = decimals === 2 ? 100 : 10
- return formatter(Intl.NumberFormat, dateLocale(), decimals === 2 ? NUM_2 : NUM_1).format(Math.round(n * p) / p)
+ // `+ 0` turns a -0 into 0: a change that rounds away ("-0.03") printed "-0".
+ return formatter(Intl.NumberFormat, dateLocale(), decimals === 2 ? NUM_2 : NUM_1).format(Math.round(n * p) / p + 0)
}
const NUM_1 = { maximumFractionDigits: 1 }
const NUM_2 = { maximumFractionDigits: 2 }