import { type ClassValue, clsx } from 'clsx'
import { twMerge } from 'tailwind-merge'
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs))
}
/**
* Format timestamp to HH:MM:SS.mmm
*/
export function formatTimestamp(timestamp: number): string {
const date = new Date(timestamp / 1000) // Convert from microseconds
const hours = date.getHours().toString().padStart(2, '0')
const minutes = date.getMinutes().toString().padStart(2, '0')
const seconds = date.getSeconds().toString().padStart(2, '0')
const ms = date.getMilliseconds().toString().padStart(3, '0')
return `${hours}:${minutes}:${seconds}.${ms}`
}
/**
* Truncate string with ellipsis
*/
export function truncate(str: string, length: number): string {
if (str.length <= length) return str
return str.slice(0, length) + '...'
}
/**
* Format duration in microseconds to human readable
*/
export function formatDuration(micros: number): string {
if (micros < 1000) return `${micros}μs`
if (micros < 1000000) return `${(micros / 1000).toFixed(2)}ms`
return `${(micros / 1000000).toFixed(2)}s`
}
/**
* Pretty print JSON with syntax highlighting
*/
export function syntaxHighlightJSON(json: string): string {
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
return json.replace(
/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
(match) => {
let cls = 'json-number'
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'json-key'
} else {
cls = 'json-string'
}
} else if (/true|false/.test(match)) {
cls = 'json-boolean'
} else if (/null/.test(match)) {
cls = 'json-null'
}
return '<span class="' + cls + '">' + match + '</span>'
}
)
}