Skip to content

Commit eccca89

Browse files
juanjoDiazknownasilya
authored andcommitted
fix: Support empty array with opts.fields (#281)
1 parent e07d315 commit eccca89

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

lib/JSON2CSVParser.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class JSON2CSVParser extends JSON2CSVBase {
4040
preprocessData(data) {
4141
const processedData = Array.isArray(data) ? data : [data];
4242

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

lib/JSON2CSVTransform.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class JSON2CSVTransform extends Transform {
2727
this.push('\ufeff');
2828
}
2929

30+
if (this.opts.fields) {
31+
this.pushHeader();
32+
}
33+
3034
}
3135

3236
/**
@@ -120,6 +124,18 @@ class JSON2CSVTransform extends Transform {
120124
done();
121125
}
122126

127+
/**
128+
* Generate the csv header and pushes it downstream.
129+
*/
130+
pushHeader() {
131+
if (this.opts.header) {
132+
const header = this.getHeader(this.opts);
133+
this.emit('header', header);
134+
this.push(header);
135+
this._hasWritten = true;
136+
}
137+
}
138+
123139
/**
124140
* Transforms an incoming json data to csv and pushes it downstream.
125141
*
@@ -130,12 +146,7 @@ class JSON2CSVTransform extends Transform {
130146

131147
if (!this._hasWritten) {
132148
this.opts.fields = this.opts.fields || Object.keys(processedData[0]);
133-
if (this.opts.header) {
134-
const header = this.getHeader(this.opts);
135-
this.emit('header', header);
136-
this.push(header);
137-
this._hasWritten = true;
138-
}
149+
this.pushHeader();
139150
}
140151

141152
processedData.forEach(row => {

test/JSON2CSVParser.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
3737
t.end();
3838
});
3939

40+
testRunner.add('should handle empty array', (t) => {
41+
const opts = {
42+
fields: ['carModel', 'price', 'color']
43+
};
44+
45+
const parser = new Json2csvParser(opts);
46+
const csv = parser.parse(jsonFixtures.emptyArray);
47+
48+
t.equal(csv, csvFixtures.emptyObject);
49+
t.end();
50+
});
51+
4052
testRunner.add('should hanlde array with nulls', (t) => {
4153
const input = [null];
4254
const opts = {

test/JSON2CSVTransform.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,24 @@ module.exports = (testRunner, jsonFixtures, csvFixtures) => {
9797
.on('error', err => t.notOk(true, err.message));
9898
});
9999

100+
testRunner.add('should handle empty array', (t) => {
101+
const opts = {
102+
fields: ['carModel', 'price', 'color']
103+
};
104+
105+
const transform = new Json2csvTransform(opts);
106+
const processor = jsonFixtures.emptyArray().pipe(transform);
107+
108+
let csv = '';
109+
processor
110+
.on('data', chunk => (csv += chunk.toString()))
111+
.on('end', () => {
112+
t.equal(csv, csvFixtures.emptyObject);
113+
t.end();
114+
})
115+
.on('error', err => t.notOk(true, err.message));
116+
});
117+
100118
testRunner.add('should hanlde array with nulls', (t) => {
101119
const input = new Readable();
102120
input._read = () => {};

test/fixtures/json/emptyArray.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]

0 commit comments

Comments
 (0)