mirror of
https://github.com/Novattz/creamlinux-installer.git
synced 2026-01-28 22:32:49 -05:00
Initial changes
This commit is contained in:
85
src/utils/helpers.ts
Normal file
85
src/utils/helpers.ts
Normal 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
1
src/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './helpers';
|
||||
Reference in New Issue
Block a user