/** * 生成唯一ID * @returns 唯一标识符 */ export function generateId(): string { return `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`; } /** * 格式化时间戳 * @param date 日期对象 * @returns 格式化的时间字符串 */ export function formatTimestamp(date: Date): string { const now = new Date(); const diff = now.getTime() - date.getTime(); // 小于1分钟显示"刚刚" if (diff < 60000) { return '刚刚'; } // 小于1小时显示分钟 if (diff < 3600000) { const minutes = Math.floor(diff / 60000); return `${minutes}分钟前`; } // 小于24小时显示小时 if (diff < 86400000) { const hours = Math.floor(diff / 3600000); return `${hours}小时前`; } // 超过24小时显示具体时间 return date.toLocaleString('zh-CN', { month: 'short', day: 'numeric', hour: '2-digit', minute: '2-digit' }); } /** * 检测文本中的URL * @param text 文本内容 * @returns URL数组 */ export function detectUrls(text: string): string[] { const urlRegex = /(https?:\/\/[^\s]+)/g; return text.match(urlRegex) || []; } /** * 将文本中的URL转换为链接 * @param text 文本内容 * @returns 包含链接的HTML字符串 */ export function linkifyText(text: string): string { const urlRegex = /(https?:\/\/[^\s]+)/g; return text.replace(urlRegex, '$1'); } /** * 检测代码块 * @param text 文本内容 * @returns 是否包含代码块 */ export function hasCodeBlock(text: string): boolean { return text.includes('```') || text.includes('`'); } /** * 提取代码块语言 * @param text 文本内容 * @returns 代码语言 */ export function extractCodeLanguage(text: string): string { const match = text.match(/```(\w+)/); return match ? match[1] : 'text'; } /** * 延迟函数 * @param ms 延迟毫秒数 * @returns Promise */ export function delay(ms: number): Promise { return new Promise(resolve => setTimeout(resolve, ms)); } /** * 防抖函数 * @param func 要防抖的函数 * @param wait 等待时间 * @returns 防抖后的函数 */ export function debounce any>( func: T, wait: number ): (...args: Parameters) => void { let timeout: ReturnType; return (...args: Parameters) => { clearTimeout(timeout); timeout = setTimeout(() => func(...args), wait); }; }