79 lines
1.9 KiB
TypeScript
79 lines
1.9 KiB
TypeScript
import { clsx, type ClassValue } from 'clsx';
|
|
import { twMerge } from 'tailwind-merge';
|
|
|
|
export function cn(...inputs: ClassValue[]) {
|
|
return twMerge(clsx(inputs));
|
|
}
|
|
|
|
export function formatDuration(nanoseconds: number): string {
|
|
const ms = nanoseconds / 1_000_000;
|
|
|
|
if (ms < 1) {
|
|
return `${(nanoseconds / 1000).toFixed(0)}μs`;
|
|
}
|
|
if (ms < 1000) {
|
|
return `${ms.toFixed(1)}ms`;
|
|
}
|
|
if (ms < 60000) {
|
|
return `${(ms / 1000).toFixed(2)}s`;
|
|
}
|
|
return `${(ms / 60000).toFixed(1)}m`;
|
|
}
|
|
|
|
export function formatTime(timestamp: string): string {
|
|
const date = new Date(timestamp);
|
|
const now = new Date();
|
|
const diff = now.getTime() - date.getTime();
|
|
|
|
// Within last hour, show relative time
|
|
if (diff < 3600000) {
|
|
const minutes = Math.floor(diff / 60000);
|
|
if (minutes < 1) {
|
|
return 'just now';
|
|
}
|
|
return `${minutes}m ago`;
|
|
}
|
|
|
|
// Today, show time only
|
|
if (date.toDateString() === now.toDateString()) {
|
|
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit', second: '2-digit' });
|
|
}
|
|
|
|
// Show date and time
|
|
return date.toLocaleString([], {
|
|
month: 'short',
|
|
day: 'numeric',
|
|
hour: '2-digit',
|
|
minute: '2-digit',
|
|
});
|
|
}
|
|
|
|
export function formatBytes(bytes: number): string {
|
|
if (bytes === 0) return '0 B';
|
|
|
|
const k = 1024;
|
|
const sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
|
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
|
|
|
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(2))} ${sizes[i]}`;
|
|
}
|
|
|
|
export function formatNumber(num: number): string {
|
|
if (num >= 1_000_000) {
|
|
return `${(num / 1_000_000).toFixed(1)}M`;
|
|
}
|
|
if (num >= 1_000) {
|
|
return `${(num / 1_000).toFixed(1)}K`;
|
|
}
|
|
return num.toString();
|
|
}
|
|
|
|
export function truncate(str: string, length: number): string {
|
|
if (str.length <= length) return str;
|
|
return str.slice(0, length) + '...';
|
|
}
|
|
|
|
export function generateId(): string {
|
|
return Math.random().toString(36).substr(2, 9);
|
|
}
|