From b62f8c99b31c0a00621d850b3a4c4dded3f02db3 Mon Sep 17 00:00:00 2001 From: ARREY-ETTA BESSONG EKEP OBASI Date: Wed, 22 May 2024 23:24:04 +0100 Subject: [PATCH] fix: update secondsToMediaTime to handle hours correctly and update tests --- .../secondsToMediaTime/__tests__/index.ts | 28 +++++++++++++------ src/utils/secondsToMediaTime/index.ts | 8 ++++-- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/utils/secondsToMediaTime/__tests__/index.ts b/src/utils/secondsToMediaTime/__tests__/index.ts index 802b523a2..76a9e3242 100644 --- a/src/utils/secondsToMediaTime/__tests__/index.ts +++ b/src/utils/secondsToMediaTime/__tests__/index.ts @@ -1,19 +1,31 @@ import { secondsToMediaTime } from '..' describe('secondsToMediaTime', () => { - it('converts 0 seconds to "00:00"', () => { - expect(secondsToMediaTime(0)).toEqual('00:00') + it('converts 0 seconds to "0:00"', () => { + expect(secondsToMediaTime(0)).toEqual('0:00') }) - it('converts 65 seconds to "01:05"', () => { - expect(secondsToMediaTime(65)).toEqual('01:05') + it('converts 65 seconds to "1:05"', () => { + expect(secondsToMediaTime(65)).toEqual('1:05') }) - it('converts 3600 seconds to "60:00"', () => { - expect(secondsToMediaTime(3600)).toEqual('60:00') + it('converts 3600 seconds to "1:00:00"', () => { + expect(secondsToMediaTime(3600)).toEqual('1:00:00') }) - it('converts 3665 seconds to "61:05"', () => { - expect(secondsToMediaTime(3665)).toEqual('61:05') + it('converts 3665 seconds to "1:01:05"', () => { + expect(secondsToMediaTime(3665)).toEqual('1:01:05') + }) + + it('converts 5025 seconds to "1:23:45"', () => { + expect(secondsToMediaTime(5025)).toEqual('1:23:45') + }) + + it('converts 7261 seconds to "2:01:01"', () => { + expect(secondsToMediaTime(7261)).toEqual('2:01:01') + }) + + it('handles large values correctly', () => { + expect(secondsToMediaTime(123456)).toEqual('34:17:36') }) }) diff --git a/src/utils/secondsToMediaTime/index.ts b/src/utils/secondsToMediaTime/index.ts index 711d156e2..049fd333e 100644 --- a/src/utils/secondsToMediaTime/index.ts +++ b/src/utils/secondsToMediaTime/index.ts @@ -1,9 +1,11 @@ export const secondsToMediaTime = (seconds: number): string => { - const minutes = Math.floor(seconds / 60) + const hours = Math.floor(seconds / 3600) + const minutes = Math.floor((seconds % 3600) / 60) const remainingSeconds = Math.floor(seconds % 60) - const formattedMinutes = minutes.toString().padStart(2, '0') + const formattedHours = hours > 0 ? `${hours}:` : '' + const formattedMinutes = hours > 0 ? minutes.toString().padStart(2, '0') : minutes.toString() const formattedSeconds = remainingSeconds.toString().padStart(2, '0') - return `${formattedMinutes}:${formattedSeconds}` + return `${formattedHours}${formattedMinutes}:${formattedSeconds}` }