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

Adding the ability to reference a property in a nested object in the header parameter with dot notation. #7

Closed hassanhe closed 6 years ago

hassanhe commented 6 years ago

this will prevent the need to flatten the data model when writing a nested object based data to cvs const records= [{ foo:{ bar:'bar value' } }] if you want to write foo.bar in csv you reference it in the header now with dot notation like const params = { ... header: [ { id: 'foo.bar', title: 'bar' } ] }

coveralls commented 6 years ago

Pull Request Test Coverage Report for Build 32


Changes Missing Coverage Covered Lines Changed/Added Lines %
lib/csv-stringifiers/object.js 6 7 85.71%
<!-- Total: 6 7 85.71% -->
Totals Coverage Status
Change from base Build 31: 1.4%
Covered Lines: 60
Relevant Lines: 67

💛 - Coveralls
ryu1kn commented 6 years ago

Thank you @hassangit for the PR and sorry for my slow response.

I see the point of the enhancement but I'm kind of inclined to keep the current behaviour with the following reasons:

I'm curious to see what code made you think you need this enhancement. With the concrete sample code, it might be easier to see the benefit.

hassanhe commented 6 years ago

@ryu1kn yes you're right about the single responsibility of the library, and the reason this enhancement came to my mind is after i had to flatten the data in order to use this library for my data model, and that annoyed me, i felt that the library lakes this simple feature to access the nested property with just a dot instead of several lines of code that i have to maintain, though i am with the library implementing this little step it benefits as a user a lot. and for your third point i didn't understand the {a: {b: 'value'}} way of referencing, if you are convinced of my point of view and want to implement this feature could you tell me an example of referencing that way to make sure of implementing it in future PR. and i saw you are busy with other newer ✨ good repo's of your's so thanks 👍

ryu1kn commented 6 years ago

@hassangit Thanks!

With the 3rd point, I meant the issue we would face if the user wants to write a value whose key contains ., like:

const csvWriter = createCsvWriter({
    path: './file.csv',
    header: ['a.b']
});
csvWriter.writeRecords([{'a.b': 'value'}])  // Passing an object that has string 'a.b' as a key

The user expects to see the value in the CSV but they would get an empty string.

ryu1kn commented 6 years ago

@hassangit I'm closing this PR for now, but thanks for your time and consideration for the contribution!