73 lines
2.4 KiB
TypeScript

// 格式化相对时间
export function formatRelativeTime(date: string | Date): string {
const now = new Date();
const target = new Date(date);
const diffMs = now.getTime() - target.getTime();
const diffSeconds = Math.floor(diffMs / 1000);
const diffMinutes = Math.floor(diffSeconds / 60);
const diffHours = Math.floor(diffMinutes / 60);
const diffDays = Math.floor(diffHours / 24);
const diffMonths = Math.floor(diffDays / 30);
const diffYears = Math.floor(diffDays / 365);
if (diffYears > 0) return `${diffYears}年前`;
if (diffMonths > 0) return `${diffMonths}月前`;
if (diffDays > 0) return `${diffDays}天前`;
if (diffHours > 0) return `${diffHours}小时前`;
if (diffMinutes > 0) return `${diffMinutes}分钟前`;
return "刚刚";
}
// 格式化时间显示 (mm:ss)
export function formatTime(seconds: number): string {
const mins = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${mins}:${secs.toString().padStart(2, "0")}`;
}
// 处理评论文本中的表情占位符
export function parseCommentText(text: string): (string | { type: "emoji"; name: string })[] {
const parts: (string | { type: "emoji"; name: string })[] = [];
const regex = /\[([^\]]+)\]/g;
let lastIndex = 0;
let match: RegExpExecArray | null;
while ((match = regex.exec(text)) !== null) {
// 添加表情前的文本
if (match.index > lastIndex) {
parts.push(text.slice(lastIndex, match.index));
}
// 添加表情
parts.push({ type: "emoji", name: match[1] });
lastIndex = regex.lastIndex;
}
// 添加剩余文本
if (lastIndex < text.length) {
parts.push(text.slice(lastIndex));
}
return parts;
}
// 从 localStorage 获取数值
export function getNumberFromStorage(key: string, defaultValue: number): number {
if (typeof window === "undefined") return defaultValue;
const saved = localStorage.getItem(key);
if (!saved) return defaultValue;
const parsed = parseFloat(saved);
return Number.isNaN(parsed) ? defaultValue : parsed;
}
// 从 localStorage 获取字符串
export function getStringFromStorage(key: string, defaultValue: string): string {
if (typeof window === "undefined") return defaultValue;
return localStorage.getItem(key) || defaultValue;
}
// 保存到 localStorage
export function saveToStorage(key: string, value: string | number): void {
if (typeof window === "undefined") return;
localStorage.setItem(key, value.toString());
}