Skip to content

Commit

Permalink
fix: Support empty array with opts.fields (#281)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Apr 10, 2018
1 parent e07d315 commit eccca89
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
2 changes: 1 addition & 1 deletion lib/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class JSON2CSVParser extends JSON2CSVBase {
preprocessData(data) {
const processedData = Array.isArray(data) ? data : [data];

if (!this.opts.fields && processedData.length === 0 || typeof processedData[0] !== 'object') {
if (!this.opts.fields && (processedData.length === 0 || typeof processedData[0] !== 'object')) {
throw new Error('Data should not be empty or the "fields" option should be included');
}

Expand Down
23 changes: 17 additions & 6 deletions lib/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class JSON2CSVTransform extends Transform {
this.push('\ufeff');
}

if (this.opts.fields) {
this.pushHeader();
}

}

/**
Expand Down Expand Up @@ -120,6 +124,18 @@ class JSON2CSVTransform extends Transform {
done();
}

/**
* Generate the csv header and pushes it downstream.
*/
pushHeader() {
if (this.opts.header) {
const header = this.getHeader(this.opts);
this.emit('header', header);
this.push(header);
this._hasWritten = true;
}
}

/**
* Transforms an incoming json data to csv and pushes it downstream.
*
Expand All @@ -130,12 +146,7 @@ class JSON2CSVTransform extends Transform {

if (!this._hasWritten) {
this.opts.fields = this.opts.fields || Object.keys(processedData[0]);
if (this.opts.header) {
const header = this.getHeader(this.opts);
this.emit('header', header);
this.push(header);
this._hasWritten = true;
}
this.pushHeader();
}

processedData.forEach(row => {
Expand Down
12 changes: 12 additions & 0 deletions test/JSON2CSVParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
t.end();
});

testRunner.add('should handle empty array', (t) => {
const opts = {
fields: ['carModel', 'price', 'color']
};

const parser = new Json2csvParser(opts);
const csv = parser.parse(jsonFixtures.emptyArray);

t.equal(csv, csvFixtures.emptyObject);
t.end();
});

testRunner.add('should hanlde array with nulls', (t) => {
const input = [null];
const opts = {
Expand Down
18 changes: 18 additions & 0 deletions test/JSON2CSVTransform.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
.on('error', err => t.notOk(true, err.message));
});

testRunner.add('should handle empty array', (t) => {
const opts = {
fields: ['carModel', 'price', 'color']
};

const transform = new Json2csvTransform(opts);
const processor = jsonFixtures.emptyArray().pipe(transform);

let csv = '';
processor
.on('data', chunk => (csv += chunk.toString()))
.on('end', () => {
t.equal(csv, csvFixtures.emptyObject);
t.end();
})
.on('error', err => t.notOk(true, err.message));
});

testRunner.add('should hanlde array with nulls', (t) => {
const input = new Readable();
input._read = () => {};
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/json/emptyArray.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit eccca89

Please sign in to comment.