Skip to content

Commit e1fb8d5

Browse files
committed
fix: handle null values in csv export
1 parent b5907d8 commit e1fb8d5

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -403,7 +403,7 @@ export function toCSV(data: any[], options: CSVOptions): string {
403403
: value.toISOString()
404404
);
405405
} else {
406-
l.push((typeof value !== 'undefined' ? value : '').toString());
406+
l.push((typeof value !== 'undefined' && value !== null ? value : '').toString());
407407
}
408408
}
409409
out.push(l);

test/ts/pipeline.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,26 @@ XXX,,YYY,0,false`
204204
});
205205
});
206206

207+
it('should return the requested top level fields as csv, with empty fields also for null values', function () {
208+
csv_options = {
209+
fields: ['a', 'e', 'b', 'c', 'd'],
210+
};
211+
csv_data = [
212+
{ a: 'AAA', b: 'BBB', c: 1, d: true, e: null },
213+
{ a: 'XXX', b: 'YYY', c: 0, d: false, e: null },
214+
];
215+
return request
216+
.get('/tests')
217+
.expect(200)
218+
.expect('Content-Type', /text\/csv/)
219+
.then(({ text: data }) => {
220+
data.should.equal(
221+
`AAA,,BBB,1,true
222+
XXX,,YYY,0,false`
223+
);
224+
});
225+
});
226+
207227
it('should return the requested top level fields as csv, with quoted fields', function () {
208228
csv_options = {
209229
fields: ['a', 'b', 'c'],

0 commit comments

Comments
 (0)