Created
January 18, 2018 19:08
-
-
Save Floofies/21784daed1e53359b8af74b8b9ea4a74 to your computer and use it in GitHub Desktop.
Basic 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
// 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