ttrpg-tools/src/components/utils/csv-loader.ts

30 lines
640 B
TypeScript
Raw Normal View History

2026-02-27 12:24:51 +08:00
import { parse } from 'csv-parse/browser/esm/sync';
import type { CardData } from '../types';
/**
* CSV
*/
const csvCache = new Map<string, CardData[]>();
/**
* CSV
*/
export async function loadCSV(path: string): Promise<CardData[]> {
if (csvCache.has(path)) {
return csvCache.get(path)!;
}
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 CardData[];
csvCache.set(path, result);
return result;
}