refactor: csv loader
This commit is contained in:
@@ -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[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user