Monday, October 17, 2011

CSV parser in JavaScript

I wanted a JavaScript based CSV parser. I couldn't find a good one (that would allow multi-character delimiters, and text qualifiers), so I wrote it. The textToArray function should run under any js implementation; but the demonstration of it is written for a ringojs environment.

Call textToArray with a line of delimited text, and the delimiter and text qualifier you're expecting to find; it will give back an array of the elements it finds.
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("---"));
}