Skip to content

Instantly share code, notes, and snippets.

@black-black-cat
Last active November 7, 2019 02:35
Show Gist options
  • Save black-black-cat/41ad9f1cb82c9ea97a5ceacc1f0442c8 to your computer and use it in GitHub Desktop.
Save black-black-cat/41ad9f1cb82c9ea97a5ceacc1f0442c8 to your computer and use it in GitHub Desktop.
parseCSV csv
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>Hello world!</h1>
<script src="script.js"></script>
</body>
</html>
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;
}
/* todo: add styles */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment