-
-
Notifications
You must be signed in to change notification settings - Fork 39
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
Allow 'firstWrite' to be passed in as 'mode' #4
Conversation
f555f79
to
325abe3
Compare
Hi @jonmelcher , thank you for the PR! Do you mind sharing the use-case with me? If I were to think of a drawback of this feature, the append mode could let you add lines to a different structure csv file by mistake, which cannot happen if we're using the write mode. Is keeping the same CsvWriter instance around an option so that you can keep writing to the same file without putting an unnecessary header line? Or does the csv file already exist when your app starts up in which case you really need append mode? |
By the way, the code change itself looks good to me. I would give |
Hi @ryu1kn! I'm gathering some metrics on a web application using lighthouse, and exporting the results in the CSV format. Most likely these CSV files will live longer than the CsvWriter and so append mode at the start would be nice. I'm currently just setting _firstWrite to the result of fs.fileExists() after construction of the CsvWriter. I'll try to make the changes you're suggesting soon :) |
Thanks for sharing the context!
Yup, then we want to start with the append mode, and I feel this writing to an already existing CSV file is not an unusual use-case. I'm happy to support it.
Cool, I'm looking forward to it 😉 |
add 'append' to possible parameters to pass in: this replaces firstWrite
ba95a60
to
e01ddaa
Compare
@ryu1kn I've made the changes you've recommended i.e. changing mode to append and using boolean; I'm not quite sure what it means to only honour the append parameter if header is not required, but am happy to make those further changes with some explanation or pointing to some relevant codes; I couldn't find where any explicit checks for headers being required were in the code |
Thanks @jonmelcher ! I was thinking we should ignore The other options would be:
To sum up, the current behaviour is better 😉 |
I'm merging your change. I'll update the README, also mention to the |
Also removed one test case as it is covered in another test
Also updated changelog. Ready to publish a new version. |
Looks great @ryu1kn! Thanks for making those changes as well :) 👍 |
* Support for adding CSV records to already existing files. Thanks to @jonmelcher. [PR #4](#4) * It's been on the previous version for more than 1 year and is stable. Time to go v1.
Cool! Just published as v1.0.0. |
I have a use-case where it's useful to specify whether I am wanting to append to an already existing file on the first write.
This replaces _firstWrite with _mode, some validation to ensure that it defaults to 'w' if not passed in, otherwise it is 'w' or 'a'.
Added a couple unit tests for the new functionality, this shouldn't break the existing interface(s).