Created
May 25, 2014 03:07
-
-
Save trevordixon/19029f672b1205b58531 to your computer and use it in GitHub Desktop.
Very Simple Javascript CSV Parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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