Code coverage report for lib/csv.js

Statements: 88.46% (23 / 26)      Branches: 66.67% (4 / 6)      Functions: 100% (5 / 5)      Lines: 88.46% (23 / 26)      Ignored: none     

All files » lib/ » csv.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 451   1   2     1 1 1   1 1     1 1 1 3 3 3 15 15         3   1     1     1 1     1 1        
var fs = require('fs');
 
module.exports = {
    setfs: function (fsModule) {
        fs = fsModule;
    },
    parse: function (config, next) {
        var filename = config.filename;
        var opts = config.opts || {};   // options to enable configurations
        var json = [];
 
        fs.readFile(filename, function (err, data) {
            Iif (err) {
                return next(err);
            }
            var csv = data.toString().split(/\r\n|\n|\r/);
            var tokens = csv[0].split(",");
            for(var i=1;i < csv.length;i++) {
                var content = csv[i].split(",");
                var tmp = {};
                for(var j=0;j < tokens.length; j++) {
                    try {
                        tmp[tokens[j]] = content[j];
                    } catch(err) {
                        tmp[tokens[j]] = "";
                    }
                }
                json.push(tmp);
            }
            next(null, json);
        });
        
        return;
    },
    writeJsonToFile: function (config, next) {
        fs.writeFile(config.filename, JSON.stringify(config.json), function(err) {
            Iif (err) {
                return next(err);
            }
            next(null);
            return;
        });
    }
};