C2FO / fast-csv

CSV parser and formatter for node
http://c2fo.github.io/fast-csv
MIT License
1.65k stars 210 forks source link

[FEATURE] Transform Headers #566

Open daedalus28 opened 3 years ago

daedalus28 commented 3 years ago

Parsing or Formatting?

Is your feature request related to a problem? Please describe. This library is excellent - but right now, there is no way to format the headers. If I have an object with camelCase keys but want them formatted in a more friendly way, I can't use fast-csv's header support directly.

Describe the solution you'd like I've thought about this quite a bit, and I think the simplest example would be a transformHeaders function similar to the transform method used for data. It would take a header and return a new value for it. A great example use case is lodash's _.startCase.

Describe alternatives you've considered I have a wrapper around this library that I'd like to deprecate, but what I've done is implement transformHeaders which will write an object as the first "record", creating a mapping from the header strings to new values. My implementation basically takes ['a', 'b'] and writes {a: transformHeader('a'), b: transformHeader('b') }. This works but feels like something that should be handled natively by fast-csv.

Additional context As I mentioned in my other ticket, I'm trying to totally rip out all CSV stuff from contexture-export and replace it with fast-csv. With transformHeaders and an object array row type, we can make it happen!

daedalus28 commented 3 years ago

I've got a WIP wrapper on my branch that implements all of the features I'd like to see added to fast-csv. My wrapper probably doesn't quite work in the general case (which is why it's not a PR) since I was only concerned with my use case of arrays of objects, but it might be helpful to take a look a the file:

https://github.com/smartprocure/contexture-export/blob/refactoring/src/modern/fast-csv-wrapper.js