import { parse } from 'csv-parse/browser/esm/sync'; /** * 全局缓存已加载的 CSV 内容 */ const csvCache = new Map[]>(); /** * 加载 CSV 文件 * @template T 返回数据的类型,默认为 Record */ export async function loadCSV>(path: string): Promise { if (csvCache.has(path)) { return csvCache.get(path)! as T[]; } const response = await fetch(path); const content = await response.text(); const records = parse(content, { columns: true, comment: '#', trim: true, skipEmptyLines: true }); const result = records as Record[]; csvCache.set(path, result); return result as T[]; }