Initial changes

This commit is contained in:
Tickbase
2025-05-18 08:06:56 +02:00
parent 19087c00da
commit 0be15f83e7
82 changed files with 4636 additions and 3237 deletions

85
src/utils/helpers.ts Normal file
View File

@@ -0,0 +1,85 @@
/**
* General-purpose utility functions
*/
/**
* Formats a timestamp in seconds to a human-readable string
* @param seconds Number of seconds
* @returns Formatted string (e.g., "5m 30s" or "30s")
*/
export function formatTime(seconds: number): string {
if (seconds < 60) {
return `${Math.round(seconds)}s`;
}
const minutes = Math.floor(seconds / 60);
const remainingSeconds = Math.round(seconds % 60);
if (remainingSeconds === 0) {
return `${minutes}m`;
}
return `${minutes}m ${remainingSeconds}s`;
}
/**
* Truncates a string if it exceeds the specified length
* @param str String to truncate
* @param maxLength Maximum length before truncation
* @param suffix Suffix to append to truncated string (default: "...")
* @returns Truncated string
*/
export function truncateString(str: string, maxLength: number, suffix: string = '...'): string {
if (str.length <= maxLength) {
return str;
}
return str.substring(0, maxLength - suffix.length) + suffix;
}
/**
* Debounces a function to limit how often it's called
* @param fn Function to debounce
* @param delay Delay in milliseconds
* @returns Debounced function
*/
export function debounce<T extends (...args: any[]) => any>(
fn: T,
delay: number
): (...args: Parameters<T>) => void {
let timer: NodeJS.Timeout | null = null;
return function(...args: Parameters<T>) {
if (timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn(...args);
}, delay);
};
}
/**
* Creates a throttled function that only invokes the provided function at most once per specified interval
* @param fn Function to throttle
* @param limit Interval in milliseconds
* @returns Throttled function
*/
export function throttle<T extends (...args: any[]) => any>(
fn: T,
limit: number
): (...args: Parameters<T>) => void {
let lastCall = 0;
return function(...args: Parameters<T>) {
const now = Date.now();
if (now - lastCall < limit) {
return;
}
lastCall = now;
return fn(...args);
};
}

1
src/utils/index.ts Normal file
View File

@@ -0,0 +1 @@
export * from './helpers';