Skip to content

Instantly share code, notes, and snippets.

@Floofies
Created January 18, 2018 19:08
Show Gist options
  • Save Floofies/21784daed1e53359b8af74b8b9ea4a74 to your computer and use it in GitHub Desktop.
Save Floofies/21784daed1e53359b8af74b8b9ea4a74 to your computer and use it in GitHub Desktop.
Basic CSV Parser
// Parses a CSV string into a Row-first array.
function parseCsv(csvString) {
const rows = [[]];
var curRow = rows[0];
var code;
for (var loc = 0; loc < csvString.length; loc++) {
code = csvString.charCodeAt(loc);
if (code === 10 && loc !== csvString.length - 1) {
// Add a Row
curRow = [];
rows.push(curRow);
continue;
} else if (code === 44) {
// End an Element
curRow.push([]);
continue;
}
// Add a Character to an Element
curRow[curRow.length - 1] += csvString[loc];
}
return rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment