-
-
Notifications
You must be signed in to change notification settings - Fork 41
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
Wrap string values with new lines in quotes #110
Comments
Hi @okmanideep , I’ve been working on my own fork of this project called csv-writer-portable, where I've addressed this exact problem. First off, the CSV generated by the original version isn't actually invalid. I tested it using the CSV Validator Tool, and it checks out fine. But I totally get that just being valid isn't always enough; we need it to handle things the way we expect. In import { createObjectCsvWriter } from 'csv-writer-portable';
const csvPath = 'test.csv';
const csvWriter = createObjectCsvWriter({
path: csvPath,
header: [
{ id: 'phone_number', title: 'phone_number' },
{ id: 'name', title: 'name' }
],
filterFunction: (str) => {
// A simple regex to remove \r and \n chars
return str.replace(/[\r\n]/g, '');
},
alwaysQuote: true
});
const data = [
{ phone_number: 9978789799, name: "John \rDoe\n" },
{ phone_number: 8898988989, name: "Bob Marlin" }
];
async function writeCsv() {
await csvWriter.writeRecords(data);
}
writeCsv().catch(err => console.error('Error writing CSV:', err)); This way, you can clean up your strings however you need before they get written to the file. Also, Feel free to check it out on npm. Hopefully, it helps you out with your issue! Cheers, Harris |
@brakmic My bad. My repro script was not correct. The issue is visible only if there is another column after the column with Although there is value in a custom filter function for |
I tested it with the latest version of import { createObjectCsvWriter } from 'csv-writer-portable';
const csvPath = 'test.csv';
const csvWriter = createObjectCsvWriter({
path: csvPath,
header: [
{ id: 'phone_number', title: 'phone_number' },
{ id: 'name', title: 'name' }
],
filterFunction: (value: any) => {
const str = String(value);
// a simple regex to remove \r and \n chars
return str.replace(/[\r\n]/g, '');
},
alwaysQuote: true
});
let data = [{ phone_number: 9978789799, name: "John Doe \r", email: "[email protected]" },
{phone_number: 8898988989, name: "Bob Marlin", email: "[email protected]"}
]
async function writeCsv() {
await csvWriter.writeRecords(data);
}
writeCsv().catch(err => console.error('Error writing CSV:', err)); test.csv output:
|
Use |
Reproduction code
The resultant output is not a valid CSV. Checked with CSV Validator Tool
The output
The text was updated successfully, but these errors were encountered: