96493,TDM,"Surrak, Elusive Hunter",rare,G,3,Legendary Creature - Human Warrior,False
in this code
code
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (line) {
const columns = line.split(",");
if (columns.length >= 3) {
const id = columns[0];
const name = columns[2];
cardMap.set(id, name);
}
}
}
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The problem is that the CSV field for the card name contains an embedded comma and is wrapped in quotes:
code
96493,TDM,"Surrak, Elusive Hunter",rare,G,3,Legendary Creature - Human Warrior,False
When you use line.split(","), it splits on every comma. This means that the name field "Surrak, Elusive Hunter" will be split into two separate parts ("Surrak and Elusive Hunter"), which is not what you want.
Two Common Approaches
Use a CSV parsing library:
Libraries like Papa Parse or csv-parse are built to handle such cases. They support quoted values, escaping, and many other nuances of CSV data. Using Papa Parse might look like this:
js
// Assuming 'csvData' is your CSV string:
Papa.parse(csvData, {
header: false,
skipEmptyLines: true,
complete: function(results) {
const rows = results.data;
const cardMap = new Map();
// start at 1 if the first row is header:
for (let i = 1; i < rows.length; i++) {
const row = rows[i];
if (row.length >= 3) {
const id = row[0];
const name = row[2];
cardMap.set(id, name);
}
}
// cardMap is ready for use
}
});
Write a custom CSV parsing function:
If you prefer not to use an external library, you can write a simple parser that handles quoted fields. For example:
js
function parseCSVLine(line) {
const result = [];
let current = '';
let insideQuotes = false;
for (let i = 0; i < line.length; i++) {
const char = line[i];
if (char === '"' && line[i + 1] === '"') {
// Escaped double quote ("" -> ")
current += '"';
i++; // Skip the next quote
} else if (char === '"') {
// Toggle insideQuotes flag
insideQuotes = !insideQuotes;
} else if (char === ',' && !insideQuotes) {
// End of a field
result.push(current);
current = '';
} else {
current += char;
}
}
result.push(current); // Add the last field
return result;
}
// Then use this function in your loop:
const cardMap = new Map();
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (line) {
const columns = parseCSVLine(line);
if (columns.length >= 3) {
const id = columns[0];
const name = columns[2];
cardMap.set(id, name);
}
}
}
Explanation
Using a Library:
The library approach offloads all the edge cases (like escaped quotes or newlines within quoted fields) to a well-tested tool.
Custom Parser:
The custom function parseCSVLine works by tracking whether it is inside a quoted section. When it encounters a comma that is not within quotes, it treats that as a field separator. It also handles escaped quotes (i.e., "" which represents a literal " in the field).
Either approach will correctly handle the CSV line:
code
96493,TDM,"Surrak, Elusive Hunter",rare,G,3,Legendary Creature - Human Warrior,False
so that "Surrak, Elusive Hunter" remains a single field. Pick the approach that best suits your project.