Cleanup and Export
Quoted Field Parser
A small parser keeps commas inside quoted fields while splitting a CSV row into cells.
Program
CSV files often contain commas inside quoted values. A parser has to track quote state before splitting on commas.
quoted_field_parser.html
<pre id="raw">name,notes
Ada,"ships, reviews"</pre>
<table id="grid"></table>
<script>
const raw = document.querySelector("#raw");
const grid = document.querySelector("#grid");
const row = raw.textContent.trim().split("\n")[1];
let quoted = false;
const cells = row.split("").reduce((parts, char) => {
if (char === "\"") quoted = !quoted;
else if (char === "," && !quoted) parts.push("");
else parts[parts.length - 1] += char;
return parts;
}, [""]);
grid.innerHTML = "<tr>" + cells.map(cell => `<td>${cell}</td>`).join("") + "</tr>";
</script>
quoted fields
A quoted CSV field can contain commas that are data, not delimiters.
parser state
The parser needs state to know whether it is inside quotes.