ryu1kn / csv-writer

Convert objects/arrays into a CSV string or write them into a CSV file
https://www.npmjs.com/package/csv-writer
MIT License
246 stars 39 forks source link

Options for Enclosure, Delimiter & Escape #21

Closed slpixe closed 5 years ago

slpixe commented 5 years ago

Hi, Really nice library, and its cool that it supports arrays and objects, and mapping of headers 👍

Seen a few similar previous issues regarding the Delimiter, but I was curious about having enclosures e.g. "value"

I can see you already have a

needsQuote()

for having enclosures for fields that would require it, but it be pretty nice to have the option for all fields to have this.

Although CSV isn't a defined spec, it is mentioned on RFC 4180,

Each field may or may not be enclosed in double quotes

and a other languages library(ies) have supported these options: [1] https://github.com/thephpleague/csv

My thoughts would be having the enclosure be optional, and if undefined and a field needs quoting it gets quoted as currently works. But that if a enclosure of " is passed through it could wrap all fields.

More information here: https://csv.thephpleague.com/9.0/connections/controls/

ryu1kn commented 5 years ago

Hello @slpixe , thanks for the suggestion.

So, you want an option to always double quote all field values. Do you mind sharing why you want that option?

vergissberlin commented 5 years ago

In my case, I want to create test data that is very close to the original one. The original uses quotes for every field.

André

phillipplum commented 5 years ago

Hey @ryu1kn, thanks for the nice library. I also need this option. The external software for which we write the CSV does not read the CSV correctly without quotation marks.

ryu1kn commented 5 years ago

Already 3 people raising this. (Sorry I somehow missed your comment, @vergissberlin ...) Seems like some programmes are picky on this...

And as @slpixe pointed, from RFC 4180 perspective, quoting all fields is possible too.

Each field may or may not be enclosed in double quotes

I was kind of reluctant to introduce this as I think it's parser's responsibility to deal with all valid CSVs (plus I didn't want to complicate csv-writer even it would be minor). But given the reality, I think it's reasonable to allow csv-writer to produce fields always with quotes.

ryu1kn commented 5 years ago

Introduced alwaysQuote flag. Released as v1.5.0.