From c5cf6798954f91d4dc1a769054c43f3b72f01f70 Mon Sep 17 00:00:00 2001 From: Daniel Castillo Date: Sat, 6 Jan 2024 10:48:22 -0500 Subject: [PATCH] feat: handle common errors #12 --- src/helpers/handleTimerErrors.ts | 22 ++++++++++++++++++++++ src/helpers/index.ts | 1 + src/helpers/useInternalTimer.ts | 16 ---------------- src/hooks/useStopwatch.ts | 22 ++++++---------------- src/hooks/useTimer.ts | 22 ++++++---------------- 5 files changed, 35 insertions(+), 48 deletions(-) create mode 100644 src/helpers/handleTimerErrors.ts diff --git a/src/helpers/handleTimerErrors.ts b/src/helpers/handleTimerErrors.ts new file mode 100644 index 0000000..afcb7e1 --- /dev/null +++ b/src/helpers/handleTimerErrors.ts @@ -0,0 +1,22 @@ +export const handleTimerErrors = ( + days: number, + hours: number, + minutes: number, + seconds: number +) => { + if (days < 0) { + throw new Error("The days parameter has to be more or equal than 0."); + } else if (hours < 0 || hours >= 24) { + throw new Error( + "The hours parameter has to be more or equal than 0 or less than 24." + ); + } else if (minutes < 0 || minutes >= 60) { + throw new Error( + "The minutes parameter has to be more or equal than 0 or less than 60." + ); + } else if (seconds < 0 || seconds >= 60) { + throw new Error( + "The seconds parameter has to be more or equal than 0 or less than 60." + ); + } +}; diff --git a/src/helpers/index.ts b/src/helpers/index.ts index ff2dc4e..5a468de 100644 --- a/src/helpers/index.ts +++ b/src/helpers/index.ts @@ -2,3 +2,4 @@ export * from "./addLeadingZero"; export * from "./useInternalStopwatch"; export * from "./useInternalTimer"; export * from "./useCounter"; +export * from "./handleTimerErrors"; diff --git a/src/helpers/useInternalTimer.ts b/src/helpers/useInternalTimer.ts index 1a07d39..2afb881 100644 --- a/src/helpers/useInternalTimer.ts +++ b/src/helpers/useInternalTimer.ts @@ -10,22 +10,6 @@ export const useInternalTimer = ( startPaused?: boolean, separator?: string ): InternalCounter => { - if (days < 0) { - throw new Error("The days parameter has to be more or equal than 0."); - } else if (hours < 0 || hours >= 24) { - throw new Error( - "The hours parameter has to be more or equal than 0 and less than 24." - ); - } else if (minutes < 0 || minutes >= 60) { - throw new Error( - "The minutes parameter has to be more or equal than 0 and less than 60." - ); - } else if (seconds < 0 || seconds >= 60) { - throw new Error( - "The seconds parameter has to be more or equal than 0 and less than 60." - ); - } - const [time, setTime] = useState({ days, hours, minutes, seconds }); const [paused, setPaused] = useState(startPaused ?? false); const divider = separator ?? ":"; diff --git a/src/hooks/useStopwatch.ts b/src/hooks/useStopwatch.ts index a0611ea..c0b212d 100644 --- a/src/hooks/useStopwatch.ts +++ b/src/hooks/useStopwatch.ts @@ -1,5 +1,9 @@ import { BaseCounter, BaseCounterStatus, Zero } from "../interfaces"; -import { useInternalStopwatch, useInternalTimer } from "../helpers"; +import { + useInternalStopwatch, + useInternalTimer, + handleTimerErrors, +} from "../helpers"; interface Stopwatch extends BaseCounter, BaseCounterStatus { remainingTime: Zero; @@ -13,21 +17,7 @@ export const useStopwatch = ( startPaused?: boolean, separator?: string ): Stopwatch => { - if (days < 0) { - throw new Error("The days parameter has to be more or equal than 0."); - } else if (hours < 0 || hours >= 24) { - throw new Error( - "The hours parameter has to be more or equal than 0 or less than 24." - ); - } else if (minutes < 0 || minutes >= 60) { - throw new Error( - "The minutes parameter has to be more or equal than 0 or less than 60." - ); - } else if (seconds < 0 || seconds >= 60) { - throw new Error( - "The seconds parameter has to be more or equal than 0 or less than 60." - ); - } + handleTimerErrors(days, hours, minutes, seconds); const stopwatch = useInternalStopwatch( days, diff --git a/src/hooks/useTimer.ts b/src/hooks/useTimer.ts index 880c686..6b88cef 100644 --- a/src/hooks/useTimer.ts +++ b/src/hooks/useTimer.ts @@ -1,5 +1,9 @@ import { BaseCounter, BaseCounterStatus, Zero } from "../interfaces"; -import { useInternalStopwatch, useInternalTimer } from "../helpers"; +import { + useInternalStopwatch, + useInternalTimer, + handleTimerErrors, +} from "../helpers"; export interface Timer extends BaseCounter, BaseCounterStatus { elapsedTime: Zero; @@ -13,21 +17,7 @@ export const useTimer = ( startPaused?: boolean, separator?: string ): Timer => { - if (days < 0) { - throw new Error("The days parameter has to be more or equal than 0."); - } else if (hours < 0 || hours >= 24) { - throw new Error( - "The hours parameter has to be more or equal than 0 and less than 24." - ); - } else if (minutes < 0 || minutes >= 60) { - throw new Error( - "The minutes parameter has to be more or equal than 0 and less than 60." - ); - } else if (seconds < 0 || seconds >= 60) { - throw new Error( - "The seconds parameter has to be more or equal than 0 and less than 60." - ); - } + handleTimerErrors(days, hours, minutes, seconds); const timer = useInternalTimer( days,