Skip to content

Commit

Permalink
Ticket #141: feature request - emit header event
Browse files Browse the repository at this point in the history
Ticket #198: bug fix - using pipe cause issues.
  • Loading branch information
Keyang committed Sep 30, 2017
1 parent 41379c7 commit 5a2251c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 8 deletions.
8 changes: 4 additions & 4 deletions libs/core/Converter.js
Expand Up @@ -302,7 +302,7 @@ Converter.prototype.processHead = function (fileLine, cb) {
}
var res = linesToJson(lines.lines, params, 0);
// Put the header with the first row
if(res.length > 0) res[0].header = params._headers;
// if(res.length > 0) res[0].header = params._headers;
this.processResult(res);
this.lastIndex += res.length;
this.recordNum += res.length;
Expand Down Expand Up @@ -354,7 +354,7 @@ Converter.prototype.processResult = function (result) {

Converter.prototype.emitResult = function (r) {
var index = r.index;
var header = r.header;
var header = this.param;
var row = r.row;
var result = r.json;
var resultJson = null;
Expand All @@ -374,8 +374,8 @@ Converter.prototype.emitResult = function (r) {
this.transform(resultJson, row, index);
resultStr = null;
}
if (this._needEmitHeader && header) {
this.emit("header", header);
if (this._needEmitHeader && this.param._headers && index === 0) {
this.emit("header", this.param._headers);
}
if (this._needEmitJson) {
this.emit("json", resultJson, index);
Expand Down
10 changes: 6 additions & 4 deletions libs/core/rowSplit.js
Expand Up @@ -119,11 +119,13 @@ function _escapeQuote(segment, quote, escape) {

var key = "es|" + quote + "|" + escape;
if (cachedRegExp[key] === undefined) {
if (escape === "\\") {
escape = "\\\\";
}
cachedRegExp[key] = new RegExp(escape + quote, 'g');

// if (escape === "\\") {
// escape = "\\\\";
// }
cachedRegExp[key] = new RegExp('\\'+escape + '\\'+quote, 'g');
}
var regExp = cachedRegExp[key];
// console.log(regExp,segment);
return segment.replace(regExp, quote);
}
2 changes: 2 additions & 0 deletions test/data/pipeAsQuote
@@ -0,0 +1,2 @@
test,test2,test3
blag,blagh,|blahhh, blah|
9 changes: 9 additions & 0 deletions test/testCSVConverter2.js
Expand Up @@ -430,15 +430,23 @@ describe("CSV Converter", function () {

it("should ignore column", function(done) {
var rs = fs.createReadStream(__dirname + "/data/dataWithQoutes");
var headerEmitted=false;
csv({
ignoreColumns:[0]
})
.fromStream(rs)
.on("header", function(header) {
assert.equal(header.indexOf("TIMESTAMP"), -1);
assert.equal(header.indexOf("UPDATE"), 0);
if (headerEmitted){
throw("header event should only happen once")
}
headerEmitted=true;
})
.on("csv", function(row, idx) {
if (!headerEmitted) {
throw("header should be emitted before any data events");
}
assert(idx >= 0);
if (idx ===1){
assert.equal(row[0],"n");
Expand All @@ -449,6 +457,7 @@ describe("CSV Converter", function () {
assert(idx >= 0);
})
.on("end", function() {
assert(headerEmitted);
done();
});
});
Expand Down
12 changes: 12 additions & 0 deletions test/testCSVConverter3.js
Expand Up @@ -73,4 +73,16 @@ describe("CSV Converter", function () {
done()
});
})
it("should accept pipe as quote", function (done) {
csv({
quote:"|"
})
.fromFile(__dirname + "/data/pipeAsQuote")
.on('csv', function (csv) {
assert.equal(csv[2],"blahhh, blah");
})
.on('done', function () {
done()
});
})
});

0 comments on commit 5a2251c

Please sign in to comment.