Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const csv = require('@fast-csv/format');
const data = [
{ a: 'a1', b: 'b1' },
{ a: 'a2', b: 'b2' },
];
csv.writeToString(data, { headers: false })
.then(formattedCsv => {
console.log(formattedCsv);
process.exit();
})
.catch(err => {
console.error(err.stack);
process.exit(1);
});
// Output:
// a1,b1
// a2,b2
const csv = require('@fast-csv/format');
const rows = [
['a', 'b'],
['a1', 'b1'],
['a2', 'b2'],
];
csv.writeToString(rows).then(data => console.log(data));
// Output:
// a,b
// a1,b1
// a2,b2
const csv = require('@fast-csv/format');
const data = [
{ a: 'a1', b: 'b1' },
{ a: 'a2', b: 'b2' },
];
const transform = row => ({
A: row.a,
B: row.b,
});
csv.writeToString(data, { headers: true, transform })
.then(formattedCsv => console.log(formattedCsv))
.catch(err => console.error(err.stack));
// Output:
// A,B
// a1,b1
// a2,b2
const csv = require('@fast-csv/format');
const rows = [
['a', 'b'],
['a1', 'b1'],
['a2', 'b2'],
];
csv.writeToBuffer(rows).then(data => console.log(data.toString()));
// Output:
// a,b
// a1,b1
// a2,b2
const fs = require('fs');
const path = require('path');
const csv = require('@fast-csv/format');
const rows = [
['a', 'b'],
['a1', 'b1'],
['a2', 'b2'],
];
const filePath = path.resolve(__dirname, 'write_to_path.tmp.csv');
csv.writeToPath(filePath, rows)
.on('error', err => console.error(err))
.on('finish', () => {
console.log('File Contents:');
console.log(fs.readFileSync(filePath).toString());
});
// Output:
// File Contents:
// a,b
// a1,b1
// a2,b2
const csv = require('@fast-csv/format');
const csvStream = csv.format({ headers: true, quoteHeaders: true });
csvStream.pipe(process.stdout).on('end', process.exit);
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.end();
// Output:
// "header1","header2"
// value1a,value2a
// value1a,value2a
// value1a,value2a
// value1a,value2a
const csv = require('@fast-csv/format');
const csvStream = csv.format({ headers: true, quoteColumns: true });
csvStream.pipe(process.stdout).on('end', process.exit);
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.end();
// Output:
// "header1","header2"
// "value1a","value2a"
// "value1a","value2a"
// "value1a","value2a"
// "value1a","value2a"
const csv = require('@fast-csv/format');
const csvStream = csv.format({ headers: ['header2'] });
csvStream.pipe(process.stdout).on('end', process.exit);
csvStream.write({ header1: 'value1a', header2: 'value1b' });
csvStream.write({ header1: 'value2a', header2: 'value2b' });
csvStream.write({ header1: 'value3a', header2: 'value3b' });
csvStream.write({ header1: 'value4a', header2: 'value4b' });
csvStream.end();
// Output:
// header2
// value1b
// value2b
// value3b
// value4b
const csv = require('@fast-csv/format');
const csvStream = csv.format({
headers: true,
quoteColumns: { header1: true },
quoteHeaders: { header2: true },
});
csvStream.pipe(process.stdout).on('end', process.exit);
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.end();
// Output:
// header1,"header2"
// "value1a",value2a
const csv = require('@fast-csv/format');
const csvStream = csv.format({ headers: true, quoteColumns: { header2: true } });
csvStream.pipe(process.stdout).on('end', process.exit);
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.write({ header1: 'value1a', header2: 'value2a' });
csvStream.end();
// Output:
// header1,"header2"
// value1a,"value2a"
// value1a,"value2a"
// value1a,"value2a"
// value1a,"value2a"