Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle common errors #14

Merged
merged 1 commit into from
Jan 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/helpers/handleTimerErrors.ts
Original file line number Diff line number Diff line change
@@ -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."
);
}
};
1 change: 1 addition & 0 deletions src/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -2,3 +2,4 @@ export * from "./addLeadingZero";
export * from "./useInternalStopwatch";
export * from "./useInternalTimer";
export * from "./useCounter";
export * from "./handleTimerErrors";
16 changes: 0 additions & 16 deletions src/helpers/useInternalTimer.ts
Original file line number Diff line number Diff line change
@@ -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 ?? ":";
22 changes: 6 additions & 16 deletions src/hooks/useStopwatch.ts
Original file line number Diff line number Diff line change
@@ -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,
22 changes: 6 additions & 16 deletions src/hooks/useTimer.ts
Original file line number Diff line number Diff line change
@@ -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,