csv.js (673B)
1 import "./types.js"; 2 3 /** 4 * @param {File} file 5 * @returns {Promise<KimchiCSVRow[]>} words 6 */ 7 export async function parseCSV(file) { 8 const content = await file.text(); 9 const rows = content.split("\n"); 10 11 /** @type {KimchiCSVRow[]} */ 12 const structured = rows.filter(row => row.trim() !== "").map(row => { 13 const columns = row.split(","); 14 if (columns.length !== 2) throw new Error("Invalid CSV file: More than 2 columns"); 15 16 const [word, level] = columns; 17 18 const kimchiLevel = parseInt(level, 10); 19 if (isNaN(kimchiLevel)) throw new Error("Invalid CSV file: No level value") 20 21 return { 22 word: word, 23 kimchiLevel: kimchiLevel, 24 } 25 }); 26 27 return structured; 28 }