Skip to content

Commit

Permalink
Merge c91a422 into 7377e4a
Browse files Browse the repository at this point in the history
  • Loading branch information
CorvusCorrax committed Oct 9, 2018
2 parents 7377e4a + c91a422 commit e3b0451
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 4 deletions.
9 changes: 6 additions & 3 deletions exportCsv.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var asynk = require('asynk');
function exportCsv(param) {
var self = this;

this.rowDelimiter = param.rowDelimiter || "\n\r";
this.textDelimiter = _.isString(param.textDelimiter) ? param.textDelimiter : '"';
this.rowDelimiter = param.rowDelimiter || "\n";
this.delimiter = param.delimiter || ";";
this.showHeaders = !!param.showHeaders;
this.displayEmptyValue = param.displayEmptyValue || "";
Expand All @@ -23,8 +24,10 @@ function exportCsv(param) {
if (_.isUndefined(val) || _.isNull(val) || (_.isString(val) && !val.length)) {
return self.displayEmptyValue;
}
val = val.replace(/"/g, '""');
return '"' + val + '"';

// Should be a regex, but meta-characters (particularly $) are problematic
val = val.split(self.textDelimiter).join(self.textDelimiter + self.textDelimiter);
return self.textDelimiter + val + self.textDelimiter;
},
date: function(column, val) {
if (_.isNull(val) && column.nullable) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "csv.io",
"version": "0.1.0",
"version": "0.1.1",
"description": "Csv import and export through streams with backpressure",
"main": "index.js",
"scripts": {
Expand Down
104 changes: 104 additions & 0 deletions test/exportTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,4 +612,108 @@ var expectedResult = `1;"blabla1";` + data[0].someDate.toString() + `;123;1
});
exportCsv.end();
});

it('should use custom text delimiter ("$")', function(done) {
options.textDelimiter = '$';
var exportCsv = new ExportCsv(options);
var i = 0;

var currentData = {
id: 1,
someString: 'blabla1',
someDate: new Date('1995-01-01'),
someNumber: 123,
someBoolean: 1
};

var expectedData = '1;$blabla1$;' + data[0].someDate.toString() + ';123;1\n';

var output = exportCsv.getOutput(function(line, cb) {
i++;
assert.equal(line, expectedData, 'Returned csv string line should be correct');
cb();
});

exportCsv.write(currentData);

output
.on('error', function(err) {
assert(false, 'should not pass here');
})
.on('finish', function() {
assert.equal(i, 1, 'Should have parsed all lines');
done();
});
exportCsv.end();
});

it('should double custom text delimiter ("$") when found in string', function(done) {
options.textDelimiter = '$';
var exportCsv = new ExportCsv(options);
var i = 0;

var currentData = {
id: 1,
someString: 'bla$bla1',
someDate: new Date('1995-01-01'),
someNumber: 123,
someBoolean: 1
};

var expectedData = '1;$bla$$bla1$;' + data[0].someDate.toString() + ';123;1\n';

var output = exportCsv.getOutput(function(line, cb) {
i++;
assert.equal(line, expectedData, 'Returned csv string line should be correct');
cb();
});

exportCsv.write(currentData);

output
.on('error', function(err) {
assert(false, 'should not pass here');
})
.on('finish', function() {
assert.equal(i, 1, 'Should have parsed all lines');
done();
});
exportCsv.end();
});

it('should use custom text delimiter (empty string)', function(done) {
options.textDelimiter = '';
var exportCsv = new ExportCsv(options);
var i = 0;

var currentData = {
id: 1,
someString: 'blabla1',
someDate: new Date('1995-01-01'),
someNumber: 123,
someBoolean: 1
};

var expectedData = '1;blabla1;' + data[0].someDate.toString() + ';123;1\n';

var output = exportCsv.getOutput(function(line, cb) {
i++;
assert.equal(line, expectedData, 'Returned csv string line should be correct');
cb();
});

exportCsv.write(currentData);

output
.on('error', function(err) {
assert(false, 'should not pass here');
})
.on('finish', function() {
assert.equal(i, 1, 'Should have parsed all lines');
done();
});
exportCsv.end();
});


});

0 comments on commit e3b0451

Please sign in to comment.