refactor: csv loader

This commit is contained in:
2026-02-27 12:38:09 +08:00
parent 807ce5a406
commit a409efaf95
2 changed files with 7 additions and 30 deletions
+6 -6
View File
@@ -1,17 +1,17 @@
import { parse } from 'csv-parse/browser/esm/sync';
import type { CardData } from '../types';
/**
* 全局缓存已加载的 CSV 内容
*/
const csvCache = new Map<string, CardData[]>();
const csvCache = new Map<string, Record<string, string>[]>();
/**
* 加载 CSV 文件
* @template T 返回数据的类型,默认为 Record<string, string>
*/
export async function loadCSV(path: string): Promise<CardData[]> {
export async function loadCSV<T = Record<string, string>>(path: string): Promise<T[]> {
if (csvCache.has(path)) {
return csvCache.get(path)!;
return csvCache.get(path)! as T[];
}
const response = await fetch(path);
@@ -23,7 +23,7 @@ export async function loadCSV(path: string): Promise<CardData[]> {
skipEmptyLines: true
});
const result = records as CardData[];
const result = records as Record<string, string>[];
csvCache.set(path, result);
return result;
return result as T[];
}