// 通用工具函数
import type { Language } from './types';
export function cx(...values: Array<string | false | null | undefined>): string {
return values.filter(Boolean).join(' ');
}
export function formatBytes(bytes: number): string {
if (!bytes) return '0 B';
const units = ['B', 'KB', 'MB', 'GB'];
const index = Math.min(units.length - 1, Math.floor(Math.log(bytes) / Math.log(1024)));
const value = bytes / 1024 ** index;
return `${value >= 10 || index === 0 ? Math.round(value) : value.toFixed(1)} ${units[index]}`;
}
export function formatDate(timestamp: number, language: Language): string {
if (!timestamp) return '';
const date = new Date(timestamp);
const now = new Date();
const sameDay =
date.getFullYear() === now.getFullYear() &&
date.getMonth() === now.getMonth() &&
date.getDate() === now.getDate();
if (sameDay) {
return date.toLocaleTimeString(language === 'zh' ? 'zh-CN' : 'en-US', {
hour: '2-digit',
minute: '2-digit',
});
}
if (date.getFullYear() === now.getFullYear()) {
return date.toLocaleDateString(language === 'zh' ? 'zh-CN' : 'en-US', {
month: '2-digit',
day: '2-digit',
});
}
return date.toLocaleDateString(language === 'zh' ? 'zh-CN' : 'en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
});
}
export function formatFullDate(timestamp: number, language: Language): string {
if (!timestamp) return '';
return new Date(timestamp).toLocaleString(language === 'zh' ? 'zh-CN' : 'en-US', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
});
}
export function initialOf(text: string): string {
const trimmed = String(text || '').trim();
if (!trimmed) return '?';
const first = trimmed[0];
return /[a-z]/i.test(first) ? first.toUpperCase() : first;
}
/** 从 "Name <[email protected]>" 中提取展示名 */
export function displaySender(name: string, address: string): string {
if (name) return name;
if (address) return address.split('@')[0];
return '(unknown)';
}
/** 从邮件地址列表中提取首项展示 */
export function firstRecipient(list: string): string {
return String(list || '').split(',')[0]?.trim() || '';
}
export function deepMerge<T>(base: T, patch: unknown): T {
if (patch === null || typeof patch !== 'object' || Array.isArray(patch)) {
return (patch === undefined ? base : (patch as T));
}
const result: Record<string, unknown> = {
...(base && typeof base === 'object' && !Array.isArray(base) ? (base as Record<string, unknown>) : {}),
};
for (const [key, value] of Object.entries(patch as Record<string, unknown>)) {
const current = result[key];
if (
value &&
typeof value === 'object' &&
!Array.isArray(value) &&
current &&
typeof current === 'object' &&
!Array.isArray(current)
) {
result[key] = deepMerge(current, value);
} else if (value !== undefined) {
result[key] = value;
}
}
return result as T;
}
/** 生成 iframe 内容,禁用脚本并让链接在新窗口打开 */
export function buildMailFrame(html: string): string {
const base = '<base target="_blank">';
const style = `<style>
html,body{margin:0;padding:0;}
body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI","PingFang SC","Microsoft YaHei",sans-serif;
font-size:14px;line-height:1.65;color:#24292f;background:#fff;padding:18px 20px;word-break:break-word;}
img{max-width:100%;height:auto;}
a{color:#3b5bdb;}
table{max-width:100%;}
pre{white-space:pre-wrap;}
</style>`;
if (/<head[^>]*>/i.test(html)) {
return html.replace(/<head([^>]*)>/i, `<head$1>${base}${style}`);
}
return `<!doctype html><html><head><meta charset="utf-8">${base}${style}</head><body>${html}</body></html>`;
}
export function fileToBase64(file: File): Promise<string> {
return new Promise((resolve, reject) => {
const reader = new FileReader();
reader.onload = () => {
const result = String(reader.result || '');
resolve(result.includes(',') ? result.slice(result.indexOf(',') + 1) : result);
};
reader.onerror = () => reject(reader.error || new Error('读取文件失败'));
reader.readAsDataURL(file);
});
}