Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support null or false to omit header row #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Support null or false to omit header row #34

wants to merge 1 commit into from

Conversation

jeffandersen
Copy link

In my case, I had an array of objects but did not want the header row.

This change allows for options.header to be set to null or false to explicitly omit the header row.

@codecov
Copy link

codecov bot commented Nov 26, 2020

Welcome to Codecov 🎉

Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests.

Thanks for integrating Codecov - We've got you covered ☂️

@@ -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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants