Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions libs/core/Converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function Converter(params, options) {
this._needJson = null;
this._needEmitResult = null;
this._needEmitFinalResult = null;
this._needEmitHeader = null;
this._needEmitJson = null;
this._needPush = null;
this._needEmitCsv = null;
Expand All @@ -55,6 +56,9 @@ function Converter(params, options) {
if (this._needEmitJson === null) {
this._needEmitJson = this.listeners("json").length > 0;
}
if (this._needEmitHeader === null) {
this._needEmitHeader = this.listeners("header").length > 0;
}
if (this._needEmitCsv === null) {
this._needEmitCsv = this.listeners("csv").length > 0;
}
Expand Down Expand Up @@ -297,6 +301,8 @@ Converter.prototype.processHead = function (fileLine, cb) {
this.workerMgr.setParams(params);
}
var res = linesToJson(lines.lines, params, 0);
// Put the header with the first row
if(res.length > 0) res[0].header = params._headers;
this.processResult(res);
this.lastIndex += res.length;
this.recordNum += res.length;
Expand Down Expand Up @@ -348,6 +354,7 @@ Converter.prototype.processResult = function (result) {

Converter.prototype.emitResult = function (r) {
var index = r.index;
var header = r.header;
var row = r.row;
var result = r.json;
var resultJson = null;
Expand All @@ -367,6 +374,9 @@ Converter.prototype.emitResult = function (r) {
this.transform(resultJson, row, index);
resultStr = null;
}
if (this._needEmitHeader && header) {
this.emit("header", header);
}
if (this._needEmitJson) {
this.emit("json", resultJson, index);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
"hireable": true
}
],
"version": "1.1.7",
"version": "1.1.8",
"keywords": [
"csv",
"csv parser",
Expand Down
16 changes: 16 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,22 @@ All parameters can be used in Command Line tool.

`Converter` class defined a series of events.

### header

`header` event is emitted for each CSV file. It passes an array object which contains the names of the header row.

```js
const csv=require('csvtojson')
csv()
.on('header',(header)=>{
//header=> [header1, header2, header3]
})
```

`header` is always an array of strings without types.

`header` event will be emitted regardless of the `noHeaders` parameter setting.

### json

`json` event is emitted for each parsed CSV line. It passes JSON object and the row number of the CSV line in its callback function.
Expand Down
9 changes: 9 additions & 0 deletions test/testCSVConverter2.js
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ describe("CSV Converter", function () {
ignoreColumns:[0]
})
.fromStream(rs)
.on("header", function(header) {
assert.equal(header.indexOf("TIMESTAMP"), -1);
assert.equal(header.indexOf("UPDATE"), 0);
})
.on("csv", function(row, idx) {
assert(idx >= 0);
if (idx ===1){
Expand All @@ -455,6 +459,11 @@ describe("CSV Converter", function () {
includeColumns:[0]
})
.fromStream(rs)
.on("header", function(header) {
assert.equal(header.indexOf("TIMESTAMP"), 0);
assert.equal(header.indexOf("UPDATE"), -1);
assert.equal(header.length, 1);
})
.on("csv", function(row, idx) {
assert(idx >= 0);
assert.equal(row.length, 1);
Expand Down