Skip to content

Instantly share code, notes, and snippets.

@richard087
Created July 29, 2013 03:16
Show Gist options
  • Save richard087/6101951 to your computer and use it in GitHub Desktop.
Save richard087/6101951 to your computer and use it in GitHub Desktop.
// hosted at http://lastinfinitetentacle.blogspot.com.au/2011/10/csv-parser-in-javascript.html
var textToArray = function (txtLine, del, txtQual) {
"use strict";
var datArr = [], newStr = "";
while (txtLine.length > 0) {
if (txtLine.substr(0, txtQual.length) === txtQual) {
// get quoted block
newStr = txtLine.substr(0, txtQual.length + txtLine.indexOf(txtQual, txtQual.length));
datArr.push(newStr.substr(txtQual.length, newStr.length - txtQual.length * 2));
}
else {
// get data block
if (txtLine.indexOf(del) !== -1) {
newStr = txtLine.substr(0, txtLine.indexOf(del));
} else {
newStr = txtLine;
}
datArr.push(newStr);
}
txtLine = txtLine.substr(newStr.length + del.length, txtLine.length);
}
return datArr;
};
var fs = require('fs');
var con = require('console');
var del = ";;";
var txtQual = "\"\""; // a pair of quotes.
var file = fs.open('D:/ringojs-0.8/test.txt');
var line = "";
for (line in file) {
con.log(textToArray(line, del, txtQual).join("---"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment