33 lines
978 B
TypeScript
33 lines
978 B
TypeScript
/**
|
|
* Validate the value is within the bounds, returning true if it is within the bounds, false otherwise
|
|
*
|
|
* @param value - The value to validate
|
|
* @param min - The minimum value
|
|
* @param max - The maximum value
|
|
*
|
|
* @returns True if the value is within the bounds, false otherwise
|
|
*/
|
|
export const isWithinBounds = (value: number, min: number, max: number): boolean => {
|
|
if (value < min || value > max) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
};
|
|
|
|
/**
|
|
* Tries to execute an async function and handles any errors that occur.
|
|
* @param fn - The function to execute.
|
|
* @param onError - The callback to call if the function fails.
|
|
* @returns The result of the function.
|
|
*/
|
|
export const tryAsync = async (fn: () => unknown, onError?: (error: Error) => void): Promise<void> => {
|
|
try {
|
|
await fn();
|
|
} catch (error) {
|
|
const errorInstance = error instanceof Error ? error : new Error(`${error}`);
|
|
|
|
onError?.(errorInstance);
|
|
}
|
|
};
|