@editorialapp/datatools
Version:
A collection of dependencies organized into useful tools for working with data in ways that are common in data cleaning and in building news apps.
34 lines (25 loc) • 583 B
JavaScript
import { createWriteStream } from 'fs'
import { pipeline } from 'stream'
import { stringify } from 'csv'
export async function writeCsv (filepath, rows, options) {
const stream = createWriteStream(filepath)
const csv = stringify({
header: true
})
return new Promise((resolve, reject) => {
pipeline(csv, stream, (err) => {
if (err) {
return reject(err)
}
resolve()
})
for (const row of rows) {
csv.write(row)
}
csv.end()
})
}
export async function createCsvWriteStream (options) {
return stringify(options)
}
// TODO: read csv with csv-parse