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

[BUG] Formatting: Empty data does not output headers to Buffer #300

Closed
laurence-myers opened this issue Dec 18, 2019 · 3 comments
Closed
Assignees
Labels
enhancement formatting in release When a feature is merged in to the release branch
Milestone

Comments

@laurence-myers
Copy link

Describe the bug
When attempting to write out CSV data to a Buffer, if the array is empty, and a headers string array is provided, the headers are not output.

To Reproduce

const buffer = writeToBuffer([], { headers: ['Foo', 'Bar', 'Baz'] });
assert(buffer.length > 0); // fails

Expected behavior
If headers is set to a string array, the headers should be output in the CSV when writing to a Buffer.

Additional context
As a scenario: when generating daily reports, sometimes you will have no data, but you'll still want to produce a file with the headers, to distinguish between "we generated the CSV and there was no data" vs "oops, something went wrong generating the CSV, here's a blank file instead".

@doug-martin
Copy link
Contributor

@laurence-myers thanks for reporting. I think an option that allows you to specify writing a CSV with the headers if no data is provided would be the best option to maintain backwards compatibility.

Im thinking writeHeadersIfEmpty which will be defaulted to false.

Let me know what you think and if you have a better name for the option.

@laurence-myers
Copy link
Author

That sounds good to me. 🙂 👍

Maybe alwaysWriteHeaders?

I guess it should throw an error if alwaysWriteHeaders is true but headers is null | undefined | boolean.

@doug-martin doug-martin added this to the v3.7.0 milestone Dec 19, 2019
doug-martin added a commit that referenced this issue Dec 20, 2019
Added alwaysWriteHeaders option for #300
@doug-martin doug-martin added the in release When a feature is merged in to the release branch label Dec 20, 2019
@doug-martin doug-martin mentioned this issue Dec 21, 2019
@doug-martin
Copy link
Contributor

Released!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement formatting in release When a feature is merged in to the release branch
Projects
None yet
Development

No branches or pull requests

3 participants