Skip to content

Instantly share code, notes, and snippets.

@trevordixon
Created May 25, 2014 03:07
Show Gist options
  • Save trevordixon/19029f672b1205b58531 to your computer and use it in GitHub Desktop.
Save trevordixon/19029f672b1205b58531 to your computer and use it in GitHub Desktop.
Very Simple Javascript CSV Parser
function parseCSV(str) {
var arr = [];
var quote = false;
for (var row = col = c = 0; c < str.length; c++) {
var cc = str[c], nc = str[c+1];
arr[row] = arr[row] || [];
arr[row][col] = arr[row][col] || '';
if (cc == '"' && quote && nc == '"') { arr[row][col] += cc; ++c; continue; }
if (cc == '"') { quote = !quote; continue; }
if (cc == ',' && !quote) { ++col; continue; }
if (cc == '\n' && !quote) { ++row; col = 0; continue; }
arr[row][col] += cc;
}
return arr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment