mgv-backoffice 1.24.0 → 1.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ import {
7
7
  fmtPrice,
8
8
  fmtPct,
9
9
  fmtUsd,
10
+ fmtDuration,
10
11
  } from './format'
11
12
 
12
13
  const EM_DASH = '—'
@@ -127,3 +128,37 @@ describe('fmtUsd', () => {
127
128
  expect(fmtUsd(0)).toBe('+$0.00')
128
129
  })
129
130
  })
131
+
132
+ describe('fmtDuration', () => {
133
+ it('renders an em-dash for null/undefined/non-finite input', () => {
134
+ expect(fmtDuration(null)).toBe(EM_DASH)
135
+ expect(fmtDuration(undefined)).toBe(EM_DASH)
136
+ expect(fmtDuration(NaN)).toBe(EM_DASH)
137
+ expect(fmtDuration(Infinity)).toBe(EM_DASH)
138
+ })
139
+
140
+ it('drops zero-value leading units', () => {
141
+ expect(fmtDuration(12 * 60_000)).toBe('12m')
142
+ expect(fmtDuration((5 * 60 + 12) * 60_000)).toBe('5h 12m')
143
+ expect(fmtDuration((3 * 1440 + 5 * 60 + 12) * 60_000)).toBe('3d 5h')
144
+ })
145
+
146
+ it('caps the rendered units at maxUnits', () => {
147
+ const ms = (2 * 1440 + 5 * 60 + 13) * 60_000
148
+ expect(fmtDuration(ms, 3)).toBe('2d 5h 13m')
149
+ expect(fmtDuration(ms, 2)).toBe('2d 5h')
150
+ expect(fmtDuration(ms, 1)).toBe('2d')
151
+ })
152
+
153
+ it('keeps the hours unit when days are present even if hours are zero', () => {
154
+ expect(fmtDuration(2 * 1440 * 60_000, 3)).toBe('2d 0h 0m')
155
+ })
156
+
157
+ it('clamps negative durations to zero', () => {
158
+ expect(fmtDuration(-5000)).toBe('0m')
159
+ })
160
+
161
+ it('renders sub-minute durations as 0m', () => {
162
+ expect(fmtDuration(59_000)).toBe('0m')
163
+ })
164
+ })
@@ -87,3 +87,22 @@ export function fmtUsd(v: number): string {
87
87
  const sign = v < 0 ? '-' : '+'
88
88
  return `${sign}$${Math.abs(v).toFixed(2)}`
89
89
  }
90
+
91
+ /**
92
+ * Format a millisecond duration as compact day/hour/minute units, e.g.
93
+ * "3d 5h", "5h 12m" or "12m". Zero-value leading units are dropped;
94
+ * `maxUnits` caps how many units render (2 → "3d 5h", 3 → "3d 5h 12m").
95
+ * Renders an em-dash for null/undefined/non-finite input.
96
+ */
97
+ export function fmtDuration(ms: number | null | undefined, maxUnits = 2): string {
98
+ if (ms === null || ms === undefined || !isFinite(ms)) return EM_DASH
99
+ const totalMins = Math.floor(Math.max(0, ms) / 60_000)
100
+ const days = Math.floor(totalMins / 1440)
101
+ const hours = Math.floor((totalMins % 1440) / 60)
102
+ const mins = totalMins % 60
103
+ const parts: string[] = []
104
+ if (days > 0) parts.push(`${days}d`)
105
+ if (days > 0 || hours > 0) parts.push(`${hours}h`)
106
+ parts.push(`${mins}m`)
107
+ return parts.slice(0, maxUnits).join(' ')
108
+ }