change millisToSeconds function

This commit is contained in:
arc-alex
2025-05-07 11:42:43 +02:00
parent abd4369049
commit 68080d0bc6
3 changed files with 58 additions and 38 deletions
@@ -1,4 +1,5 @@
import type { MaybeNumber } from 'ontime-types';
import { TimerType } from 'ontime-types';
export const MILLIS_PER_SECOND = 1000;
export const MILLIS_PER_MINUTE = 1000 * 60;
@@ -25,8 +26,22 @@ function convertMillis(millis: MaybeNumber, conversion: number): number {
* @param millis
* @returns
*/
export function millisToSeconds(millis: MaybeNumber): number {
return convertMillis(millis, MILLIS_PER_SECOND);
export function millisToSeconds(
millis: MaybeNumber,
direction: TimerType.CountDown | TimerType.CountUp = TimerType.CountDown,
) {
if (millis === null) return 0;
if (direction === TimerType.CountDown) {
// 0 + is there to avoid result giving -0
return 0 + Math.ceil(millis / MILLIS_PER_SECOND);
}
if (direction === TimerType.CountUp) {
return Math.floor(millis / MILLIS_PER_SECOND);
}
throw new Error('?????');
}
/**