Last active
November 7, 2019 02:35
-
-
Save black-black-cat/41ad9f1cb82c9ea97a5ceacc1f0442c8 to your computer and use it in GitHub Desktop.
parseCSV csv
This file contains 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
<!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> |
This file contains 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; | |
} |
This file contains 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
/* todo: add styles */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment