All files / csv2json/libs/interfaces/cli main.js

0% Statements 0/23
0% Branches 0/4
0% Functions 0/6
0% Lines 0/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                   
/**
 * Convert input to process stdout
 */
 
//implementation
var Converter = require("../../core/Converter.js");
function _initConverter(){
    var csvConverter = new Converter();
    var started = false;
    var writeStream = process.stdout;
    csvConverter.on("record_parsed",function(rowJSON){
        if (started){
            writeStream.write(",\n");
        }
        writeStream.write(JSON.stringify(rowJSON));  //write parsed JSON object one by one.
        if (started === false){
            started = true;
        }
    });
    writeStream.write("[\n"); //write array symbol
 
    csvConverter.on("end_parsed",function(){
        writeStream.write("\n]"); //end array symbol
    });
    csvConverter.on("error",function(err){
        console.error(err);
        process.exit(-1);
    });
    return csvConverter;
}
function convertFile(fileName){
    var csvConverter=_initConverter();
    csvConverter.from(fileName);
}
 
function convertString(csvString){
    var csvConverter=_initConverter();
    csvConverter.from(csvString);
}
//module interfaces
module.exports.convertFile = convertFile;
module.exports.convertString = convertString;