From ecd13fdc5ade6d8be97e503800959b2186ce569f Mon Sep 17 00:00:00 2001 From: Carlos Valente Date: Mon, 8 Jan 2024 22:51:38 +0100 Subject: [PATCH] chore: cleanup utils --- .../viewers/common/__tests__/viewerUtils.test.ts | 8 ++++---- .../src/features/viewers/common/viewerUtils.ts | 14 +++++++++++++- .../viewers/minimal-timer/MinimalTimer.tsx | 4 ++-- apps/client/src/features/viewers/timer/Timer.tsx | 4 ++-- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/apps/client/src/features/viewers/common/__tests__/viewerUtils.test.ts b/apps/client/src/features/viewers/common/__tests__/viewerUtils.test.ts index 3bc08e277..f8b692ca8 100644 --- a/apps/client/src/features/viewers/common/__tests__/viewerUtils.test.ts +++ b/apps/client/src/features/viewers/common/__tests__/viewerUtils.test.ts @@ -1,21 +1,21 @@ -import { removePrependedZero } from '../viewerUtils.js'; +import { removePrefixZeroes } from '../viewerUtils.js'; describe('removePrependedZero', () => { it('should remove "00:" from the start of the string', () => { const timer = '00:10:10'; - const result = removePrependedZero(timer); + const result = removePrefixZeroes(timer); expect(result).toBe('10:10'); }); it('should remove "00:0:" from the start of the string', () => { const timer = '00:01:10'; - const result = removePrependedZero(timer); + const result = removePrefixZeroes(timer); expect(result).toBe('1:10'); }); it('should not modify the string if it does not start with "00:" or "00:0:"', () => { const timer = '10:10:10'; - const result = removePrependedZero(timer); + const result = removePrefixZeroes(timer); expect(result).toBe(timer); }); }); diff --git a/apps/client/src/features/viewers/common/viewerUtils.ts b/apps/client/src/features/viewers/common/viewerUtils.ts index cc9b133fa..8a1b47f24 100644 --- a/apps/client/src/features/viewers/common/viewerUtils.ts +++ b/apps/client/src/features/viewers/common/viewerUtils.ts @@ -28,7 +28,7 @@ export function getTimerByType(timerObject?: TimerTypeParams): number | null { * Receives a string such as 00:10:10 and removes the hours field if it is 00 * @param timer */ -export const removePrependedZero = (timer: string): string => { +export const removePrefixZeroes = (timer: string): string => { if (timer.startsWith('00:0')) { return timer.slice(4); } @@ -37,3 +37,15 @@ export const removePrependedZero = (timer: string): string => { } return timer; }; + +/** + * Receives a string such as 00:10:00 and removes the seconds field if it is 00 + * @param timer + */ +export const removePostZeroes = (timer: string): string => { + if (timer.endsWith(':00')) { + return timer.slice(0, -3); + } + + return timer; +}; diff --git a/apps/client/src/features/viewers/minimal-timer/MinimalTimer.tsx b/apps/client/src/features/viewers/minimal-timer/MinimalTimer.tsx index e4ebd924e..d12f75eb5 100644 --- a/apps/client/src/features/viewers/minimal-timer/MinimalTimer.tsx +++ b/apps/client/src/features/viewers/minimal-timer/MinimalTimer.tsx @@ -12,7 +12,7 @@ import { OverridableOptions } from '../../../common/models/View.types'; import { formatTime } from '../../../common/utils/time'; import { isStringBoolean } from '../../../common/utils/viewUtils'; import { useTranslation } from '../../../translation/TranslationProvider'; -import { getTimerByType, removePrependedZero } from '../common/viewerUtils'; +import { getTimerByType, removePrefixZeroes } from '../common/viewerUtils'; import './MinimalTimer.scss'; @@ -159,7 +159,7 @@ export default function MinimalTimer(props: MinimalTimerProps) { showSeconds: !userOptions.hideTimerSeconds, format: 'hh:mm:ss a', }); - display = removePrependedZero(display); + display = removePrefixZeroes(display); if (display.length < 3) { display = `${display} ${getLocalizedString('common.minutes')}`; } diff --git a/apps/client/src/features/viewers/timer/Timer.tsx b/apps/client/src/features/viewers/timer/Timer.tsx index 15fa926c8..bbe0693ef 100644 --- a/apps/client/src/features/viewers/timer/Timer.tsx +++ b/apps/client/src/features/viewers/timer/Timer.tsx @@ -15,7 +15,7 @@ import { formatTime } from '../../../common/utils/time'; import { isStringBoolean } from '../../../common/utils/viewUtils'; import { useTranslation } from '../../../translation/TranslationProvider'; import SuperscriptTime from '../common/superscript-time/SuperscriptTime'; -import { getTimerByType, removePrependedZero } from '../common/viewerUtils'; +import { getTimerByType, removePrefixZeroes } from '../common/viewerUtils'; import './Timer.scss'; @@ -123,7 +123,7 @@ export default function Timer(props: TimerProps) { showSeconds: !userOptions.hideTimerSeconds, format: 'hh:mm:ss a', }); - display = removePrependedZero(display); + display = removePrefixZeroes(display); if (display.length < 3) { display = `${display} ${getLocalizedString('common.minutes')}`; }