Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/modules/convert-array-of-objects-to-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const convertArrayOfObjectsToCSV = (data, { header, separator }) => {

array.forEach((row, idx) => {
const thisRow = Object.keys(row);
if (!header && idx === 0) {
if (header === undefined && idx === 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,

thanks for your contribution 👍

I have to dig into the project again. But just for clarification, doesnt !header does the same as header === undefined but include things like 0, null, etc...

Screenshot 2020-11-27 at 17 15 34

Copy link
Author

@jeffandersen jeffandersen Nov 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent is to allow Null or False (an explicit negative) to signify not to include the header, but when the value is not specified at all (undefined)... to automatically include the default (the current behaviour)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah okay, so if its null or undefined it should skip this line and not go into the if stmt.

Copy link
Author

@jeffandersen jeffandersen Nov 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By changing the logic to identify an explicit false we're able to have the following:

const dataArrays = [ {a: 1, b: 2}, {a: 3, b: 4} ];
const csvFromArrayOfArrays = convertArrayToCSV(dataArrays, { header: false });

With output of:

1,2
3,4

Versus the normal behavior of:

const dataArrays = [ {a: 1, b: 2}, {a: 3, b: 4} ];
const csvFromArrayOfArrays = convertArrayToCSV(dataArrays);

With output of:

a,b
1,2
3,4

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, that looks legit. I have to take a look myself, but overall I am looking forward to getting this PR merged.

thisRow.forEach((key, i) => {
const value = valueOrEmpty(key);

Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/expected-results.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ export const expectedResultArrayNoHeaderNoOptions = '1,Mark,Otto,@mdo\n2,Jacob,T

export const expectedResultObjectNoOptions = 'number,first,last,handle\n1,Mark,Otto,@mdo\n2,Jacob,Thornton,@fat\n3,Larry,the Bird,@twitter\n';

export const expectedResultObjectNoHeader = '1,Mark,Otto,@mdo\n2,Jacob,Thornton,@fat\n3,Larry,the Bird,@twitter\n';

export const expectedResultObjectOnlyHeader = 'Number,First,Last,Handle\n1,Mark,Otto,@mdo\n2,Jacob,Thornton,@fat\n3,Larry,the Bird,@twitter\n';

export const expectedResultObjectNullAndUndefined = 'Number,First,Last,Handle\n1,Mark,"",@mdo\n2,Jacob,Thornton,""\n3,Larry,the Bird,@twitter\n';
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ export const optionsHeaderSeparatorDefault = {
separator: ',',
};

export const optionsNullHeaderSeparatorDefault = {
header: null,
separator: ',',
};

export const optionsHeaderWithSpacesSeparatorDefault = {
header: ['number number', 'first', 'last', 'handle'],
separator: ',',
Expand Down
8 changes: 8 additions & 0 deletions test/modules/convert-array-of-objects-to-csv.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
optionsHeaderSeparatorSemicolon,
optionsHeaderSeparatorDefault,
optionsNullHeaderSeparatorDefault,
optionsHeaderDefaultSeparatorTab,
optionsDefault,
optionsHeaderZero,
Expand All @@ -19,6 +20,7 @@ import {
expectedResultObjectNoOptions,
expectedResultObjectHeaderSeparatorSemicolon,
expectedResultObjectOnlyHeader,
expectedResultObjectNoHeader,
expectedResultObjectOnlySeparatorTab,
expectedResultObjectNullAndUndefined,
expectedResultObjectWithDoubleQuotesInsideElement,
Expand Down Expand Up @@ -50,6 +52,12 @@ test('convertArrayOfObjectsToCSV | array of objects | options: header + default
expect(result).toBe(expectedResultObjectOnlyHeader);
});

test('convertArrayOfObjectsToCSV | array of objects | options: header = null + default separator', () => {
const result = convertArrayOfObjectsToCSV(dataObject, optionsNullHeaderSeparatorDefault);

expect(result).toBe(expectedResultObjectNoHeader);
});

test('convertArrayOfObjectsToCSV | array of objects | options: default header + separator tab', () => {
const result = convertArrayOfObjectsToCSV(dataObject, optionsHeaderDefaultSeparatorTab);

Expand Down