48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
// app/utils/format.ts - 文本格式化工具
|
|
|
|
/**
|
|
* 将选项字符串从"字母在上、内容在下"的换行格式合并为"字母. 内容"的单行格式。
|
|
* 例如:
|
|
* 输入:`A.\n错\nB.\n对`
|
|
* 输出:`A. 错\nB. 对`
|
|
*/
|
|
export const formatOptions = (options: string): string =>
|
|
// 匹配类似 "A." 开头的选项标记,将其后紧跟的换行内容合并到同一行
|
|
options.replace(/^([A-Za-z]\.)\s*\n\s*/gm, "$1 ");
|
|
|
|
/**
|
|
* 将选项字符串拆分为逐行结构,标记哪些行是正确答案。
|
|
* answer 按 # 分隔(多选),与选项正文做等值匹配(大小写不敏感)。
|
|
*/
|
|
export const getOptionLines = (
|
|
options: string,
|
|
answer: string
|
|
): { text: string; isCorrect: boolean }[] => {
|
|
const segments = answer
|
|
.split("#")
|
|
.map((s) => s.trim().toUpperCase())
|
|
.filter(Boolean);
|
|
|
|
return formatOptions(options)
|
|
.split("\n")
|
|
.filter((line) => line.trim())
|
|
.map((line) => {
|
|
const match = line.match(/^[A-Za-z]\.\s*(.*)/);
|
|
const content = match?.[1]?.trim().toUpperCase() ?? "";
|
|
const isCorrect = content !== "" && segments.includes(content);
|
|
return { text: line, isCorrect };
|
|
});
|
|
};
|
|
|
|
/** 把运行秒数格式化为 dashboard 更容易扫读的时长 */
|
|
export const formatUptime = (seconds: number) => {
|
|
const safeSeconds = Math.max(0, Math.floor(seconds));
|
|
const days = Math.floor(safeSeconds / 86_400);
|
|
const hours = Math.floor((safeSeconds % 86_400) / 3_600);
|
|
const minutes = Math.floor((safeSeconds % 3_600) / 60);
|
|
|
|
if (days > 0) return `${days} 天 ${hours} 小时`;
|
|
if (hours > 0) return `${hours} 小时 ${minutes} 分钟`;
|
|
return `${Math.max(1, minutes)} 分钟`;
|
|
};
|