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

Fix empty array literals being added when an empty array is provided and keys option is provided #264

Merged
merged 4 commits into from
Sep 26, 2024

Conversation

Altioc
Copy link
Contributor

@Altioc Altioc commented Sep 20, 2024

Background Information

  • Fixes issue(s): # Empty array literals gets added if you call json2csv with an empty array for data while also providing options.keys.

    given: converter.json2csv([], { keys: ['header1', 'header2'] });

    Before:

    header1,header2
    [],[]
    

    After:

    header1,header
    
  • Proposed release version: 5.5.6

I have...

  • added at least one test to verify the failure condition is fixed.
  • verified the tests are passing.

@@ -457,7 +457,7 @@ export const Json2Csv = function(options: FullJson2CsvOptions) {
*/
function convert(data: object[]) {
// Single document, not an array
if (utils.isObject(data) && !data.length) {
Copy link
Contributor Author

@Altioc Altioc Sep 20, 2024

Choose a reason for hiding this comment

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

All arrays are typed as object's and data.length of an empty array is 0 and !0 is true which is the source of the bug.

@mrodrig
Copy link
Owner

mrodrig commented Sep 26, 2024

Hi @Altioc, thanks for finding and opening a pull request to fix this! I'll get this merged in (don't worry about the lint failing - I'll tackle that when I run npm audit fix to patch the moderate severity dependency vuln.).

@mrodrig mrodrig merged commit dd1514d into mrodrig:main Sep 26, 2024
2 of 5 checks passed
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