Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

csvtojson

Package Overview
Dependencies
Maintainers
1
Versions
82
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

csvtojson - npm Package Compare versions

Comparing version 0.3.11 to 0.3.12

test/data/emptyFile

203

libs/core/csvConverter.js

@@ -13,121 +13,134 @@ module.exports = csvAdv;

function csvAdv(params) {
Transform.call(this);
var _param = {
"constructResult": true, //set to false to not construct result in memory. suitable for big csv data
"delimiter": ",", // change the delimiter of csv columns
"quote": "\"", //quote for a column containing delimiter.
"trim": true //trim column's space charcters
Transform.call(this);
var _param = {
"constructResult": true, //set to false to not construct result in memory. suitable for big csv data
"delimiter": ",", // change the delimiter of csv columns
"quote": "\"", //quote for a column containing delimiter.
"trim": true //trim column's space charcters
}
if (params && typeof params == "object") {
for (var key in params) {
_param[key] = params[key];
}
if (params && typeof params == "object") {
for (var key in params) {
_param[key] = params[key];
}
} else if (typeof params == "boolean") { //backcompatible with older version
console.warn("Parameter should be a JSON object like {'constructResult':false}");
_param.constructResult = params;
}
this.param = _param;
this.parseRules = [];
this.resultObject = new Result();
if (this.param.constructResult) {
this.pipe(this.resultObject);
}
this.headRow = [];
this._buffer = "";
this.rowIndex = 0;
var self = this;
} else if (typeof params == "boolean") { //backcompatible with older version
console.warn("Parameter should be a JSON object like {'constructResult':false}");
_param.constructResult = params;
}
this.param = _param;
this.parseRules = [];
this.resultObject = new Result();
if (this.param.constructResult) {
this.pipe(this.resultObject);
}
this.headRow = [];
this._buffer = "";
this.rowIndex = 0;
this._isStarted = false;
var self = this;
this._callback = null;
this.init();
return this;
this._callback = null;
this.init();
return this;
};
utils.inherits(csvAdv, Transform);
csvAdv.prototype.init = function() {
require("./init_onend.js").call(this);
require("./init_onrecord.js").call(this);
require("./init_onend.js").call(this);
require("./init_onrecord.js").call(this);
}
csvAdv.prototype._isToogleQuote = function(segment) {
var quote = this.param.quote;
var regExp = new RegExp(quote, "g");
var match = segment.toString().match(regExp);
if (match) {
return match.length % 2 != 0;
} else {
return false;
}
var quote = this.param.quote;
var regExp = new RegExp(quote, "g");
var match = segment.toString().match(regExp);
if (match) {
return match.length % 2 != 0;
} else {
return false;
}
}
csvAdv.prototype._startInit = function() {
if (this._isStarted === false) {
this.push("[" + this.getEol())
this._isStarted = true
}
}
csvAdv.prototype._transform = function(data, encoding, cb) {
var self = this;
if (encoding == "buffer") {
encoding = "utf8";
var self = this;
if (encoding == "buffer") {
encoding = "utf8";
}
this._buffer += data.toString(encoding);
if (!this.eol) {
if (this._buffer.indexOf("\r\n") > -1) { //csv from windows
this.eol = "\r\n";
} else if (this._buffer.indexOf("\n") > -1) {
this.eol = "\n";
} else if (this._buffer.indexOf("\r") > -1) {
this.eol = "\r";
} else if (this._buffer.indexOf(eol)) {
this.eol = eol;
}
this._buffer += data.toString(encoding);
if (!this.eol) {
if (this._buffer.indexOf("\r\n") > -1) { //csv from windows
this.eol = "\r\n";
} else if (this._buffer.indexOf("\n") > -1) {
this.eol = "\n";
} else if (this._buffer.indexOf("\r") > -1) {
this.eol = "\r";
} else if (this._buffer.indexOf(eol)) {
this.eol = eol;
}
this._startInit()
if (this.eol) {
//console.log(this._buffer);
if (this._buffer.indexOf(this.eol) > -1) {
var arr = this._buffer.split(this.eol);
while (arr.length > 1) {
var data = arr.shift();
if (data.length > 0) {
this.emit("record", data, this.rowIndex++);
}
}
this._buffer = arr[0];
}
if (this.eol) {
//console.log(this._buffer);
if (this._buffer.indexOf(this.eol) > -1) {
var arr = this._buffer.split(this.eol);
while (arr.length > 1) {
var data = arr.shift();
if (data.length > 0) {
this.emit("record", data, this.rowIndex++);
}
}
this._buffer = arr[0];
}
}
cb();
}
cb();
};
csvAdv.prototype._flush = function(cb) {
if (this._buffer.length != 0) { //emit last line
this.emit("record", this._buffer, this.rowIndex++, true);
}
this.push(this.eol + "]");
cb();
this._startInit();
if (this._buffer.length != 0) { //emit last line
this.emit("record", this._buffer, this.rowIndex++, true);
}
this.push(this.getEol() + "]");
cb();
};
csvAdv.prototype.getEol = function() {
return this.eol ? this.eol : eol;
}
csvAdv.prototype._headRowProcess = function(headRow) {
this.headRow = headRow;
this.parseRules = parserMgr.initParsers(headRow);
this.headRow = headRow;
this.parseRules = parserMgr.initParsers(headRow);
};
csvAdv.prototype._rowProcess = function(row, index, resultRow) {
for (var i = 0; i < this.parseRules.length; i++) {
var item = row[i];
var parser = this.parseRules[i];
var head = this.headRow[i];
parser.parse({
head: head,
item: item,
itemIndex: i,
rawRow: row,
resultRow: resultRow,
rowIndex: index,
resultObject: this.resultObject
});
}
for (var i = 0; i < this.parseRules.length; i++) {
var item = row[i];
var parser = this.parseRules[i];
var head = this.headRow[i];
parser.parse({
head: head,
item: item,
itemIndex: i,
rawRow: row,
resultRow: resultRow,
rowIndex: index,
resultObject: this.resultObject
});
}
};
csvAdv.prototype.fromString = function(csvString, cb) {
var rs = new Readable();
rs._read = function() {
this.push(csvString);
this.push(null);
}
rs.pipe(this);
if (cb && typeof cb == "function") {
this._callback = cb;
}
var rs = new Readable();
rs._read = function() {
this.push(csvString);
this.push(null);
}
rs.pipe(this);
if (cb && typeof cb == "function") {
this._callback = cb;
}
};

@@ -5,3 +5,2 @@ /**

var os = require("os");
var eol = os.EOL;
module.exports = function() {

@@ -49,3 +48,2 @@ var self = this;

self._headRowProcess(row);
self.push("[" + self.eol);
} else if (rowStr.length > 0) {

@@ -56,3 +54,3 @@ var resultRow = {};

if (started === true) {
self.push("," + self.eol);
self.push("," + self.getEol());
}

@@ -63,2 +61,2 @@ self.push(JSON.stringify(resultRow));

});
}
}

@@ -20,2 +20,2 @@ module.exports=Result;

return JSON.parse(this.buffer);
}
}

@@ -15,3 +15,3 @@ {

}],
"version": "0.3.11",
"version": "0.3.12",
"keywords": [

@@ -18,0 +18,0 @@ "csv",

@@ -138,7 +138,6 @@ var CSVAdv = require("../libs/core/csvConverter.js");

//end_parsed will be emitted once parsing finished
csvConverter.on("end_parsed", function(jsonObj) {
});
csvConverter.on("end_parsed", function(jsonObj) {});
csvConverter.fromString(data, function(err, jsonObj) {
assert(jsonObj[0].TIMESTAMP=='13954264""22',JSON.stringify(jsonObj[0].TIMESTAMP));
assert(jsonObj[1].TIMESTAMP=='abc, def, ccc',JSON.stringify(jsonObj[1].TIMESTAMP));
assert(jsonObj[0].TIMESTAMP == '13954264""22', JSON.stringify(jsonObj[0].TIMESTAMP));
assert(jsonObj[1].TIMESTAMP == 'abc, def, ccc', JSON.stringify(jsonObj[1].TIMESTAMP));
done();

@@ -154,9 +153,19 @@ });

//end_parsed will be emitted once parsing finished
csvConverter.on("end_parsed", function(jsonObj) {
});
csvConverter.on("end_parsed", function(jsonObj) {});
csvConverter.fromString(data, function(err, jsonObj) {
assert(jsonObj[0].data=="xyabcde",jsonObj);
assert(jsonObj[0].data == "xyabcde", jsonObj);
done();
});
});
it("should handle empty csv file", function(done) {
var testData = __dirname + "/data/emptyFile";
var rs = fs.createReadStream(testData);
var result = {}
var csvConverter = new CSVAdv();
csvConverter.on("end_parsed", function(jsonObj) {
assert(jsonObj.length===0)
done();
});
rs.pipe(csvConverter);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc