From 944a6cb1263b6c1c705cb885d786ac961473095f Mon Sep 17 00:00:00 2001 From: Alex Dukhno Date: Tue, 4 Sep 2018 23:30:02 +0300 Subject: [PATCH] Change time formatting for minutes from 1.50min to 1:30 (#153) --- src/lib/value-formatters.test.ts | 6 ++++-- src/lib/value-formatters.ts | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/lib/value-formatters.test.ts b/src/lib/value-formatters.test.ts index 57042b4..b147e58 100644 --- a/src/lib/value-formatters.test.ts +++ b/src/lib/value-formatters.test.ts @@ -7,7 +7,8 @@ describe('TimeFormatter', () => { expect(f.format(0.04)).toEqual('40.00µs') expect(f.format(3)).toEqual('3.00ms') expect(f.format(2070)).toEqual('2.07s') - expect(f.format(1203123)).toEqual('20.05min') + expect(f.format(150000)).toEqual('2:30') + expect(f.format(1203123)).toEqual('20:03') }) test('input units seconds', () => { @@ -15,7 +16,8 @@ describe('TimeFormatter', () => { expect(f.format(0.00004)).toEqual('40.00µs') expect(f.format(0.003)).toEqual('3.00ms') expect(f.format(2.07)).toEqual('2.07s') - expect(f.format(1203.123)).toEqual('20.05min') + expect(f.format(150)).toEqual('2:30') + expect(f.format(1203.123)).toEqual('20:03') }) }) diff --git a/src/lib/value-formatters.ts b/src/lib/value-formatters.ts index f52717c..5635eed 100644 --- a/src/lib/value-formatters.ts +++ b/src/lib/value-formatters.ts @@ -1,4 +1,5 @@ import {FileFormat} from './file-format-spec' +import {zeroPad} from './utils' export interface ValueFormatter { unit: FileFormat.ValueUnit @@ -25,7 +26,11 @@ export class TimeFormatter implements ValueFormatter { formatUnsigned(v: number) { const s = v * this.multiplier - if (s / 60 >= 1) return `${(s / 60).toFixed(2)}min` + if (s / 60 >= 1) { + const minutes = Math.floor(s / 60) + const seconds = Math.floor(s - minutes * 60).toString() + return `${minutes}:${zeroPad(seconds, 2)}` + } if (s / 1 >= 1) return `${s.toFixed(2)}s` if (s / 1e-3 >= 1) return `${(s / 1e-3).toFixed(2)}ms` if (s / 1e-6 >= 1) return `${(s / 1e-6).toFixed(2)}µs`