62 lines
1.3 KiB
TypeScript
62 lines
1.3 KiB
TypeScript
// Utility functions shared across services
|
|||
|
|
|
||
|
|
export function delay(ms: number): Promise<void> {
|
||
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||
|
|
}
|
||
|
|
|
||
|
|
export function formatBytes(bytes: number): string {
|
||
|
|
if (bytes === 0) return "0 Bytes";
|
||
|
|
const k = 1024;
|
||
|
|
const sizes = ["Bytes", "KB", "MB", "GB"];
|
||
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||
|
|
return Math.round((bytes / Math.pow(k, i)) * 100) / 100 + " " + sizes[i];
|
||
|
|
}
|
||
|
|
|
||
|
|
export function generateId(): string {
|
||
|
|
return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function isValidUrl(url: string): boolean {
|
||
|
|
try {
|
||
|
|
new URL(url);
|
||
|
|
return true;
|
||
|
|
} catch {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
export function sanitizeString(str: string): string {
|
||
|
|
return str.replace(/[<>]/g, "").trim().substring(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface PaginationParams {
|
||
|
|
page: number;
|
||
|
|
limit: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export interface PaginatedResponse<T> {
|
||
|
|
data: T[];
|
||
|
|
total: number;
|
||
|
|
page: number;
|
||
|
|
limit: number;
|
||
|
|
pages: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function calculatePagination(
|
||
|
|
total: number,
|
||
|
|
page: number,
|
||
|
|
limit: number,
|
||
|
|
): PaginatedResponse<never> {
|
||
|
|
return {
|
||
|
|
data: [],
|
||
|
|
total,
|
||
|
|
page,
|
||
|
|
limit,
|
||
|
|
pages: Math.ceil(total / limit),
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
export function getOffset(page: number, limit: number): number {
|
||
|
|
return (page - 1) * limit;
|
||
|
|
}
|