Skip to content

Instantly share code, notes, and snippets.

@wmakeev
Last active October 12, 2023 17:55
Show Gist options
  • Save wmakeev/4f000dc045630a7f9a554f6ceb317315 to your computer and use it in GitHub Desktop.
Save wmakeev/4f000dc045630a7f9a554f6ceb317315 to your computer and use it in GitHub Desktop.
Highland.js #highland #stream #csv
const fs = require('fs')
const path = require('path')
const _H = require('highland')
const csvWriter = require('csv-write-stream')
let filename = path.join(__dirname, 'out.csv')
try { fs.unlinkSync(filename) } catch (err) { }
// https://github.com/maxogden/csv-write-stream
let writer = csvWriter({ headers: ['NAME', 'EMAIL'] })
writer.pipe(fs.createWriteStream(filename))
_H(...)
// ...
.map(res => {
return [res.name, res.email]
})
.pipe(writer)
import path from "node:path";
import _H from "highland";
import { stringify } from "csv-stringify";
import { createWriteStream } from "node:fs";
import { writeFile } from "node:fs/promises";
/**
* @param {string} targetFolderPath
* @param {string} fileName
* @param {Record<string, unknown>[]} records
*/
export async function writeCsvRecords(targetFolderPath, fileName, records) {
const filePath = path.join(targetFolderPath, `${fileName}.csv`);
const recordPick = records[0];
console.log(`Writing ${fileName}.csv`);
if (!recordPick) {
await writeFile(filePath, "", "utf8");
return;
}
const headers = Object.keys(recordPick);
const stream = _H([headers])
.concat(_H(records).map((it) => headers.map((h) => String(it[h] ?? ""))))
.pipe(stringify())
.pipe(createWriteStream(filePath));
return await new Promise((resolve, reject) => {
stream.on("close", resolve);
stream.on("error", reject);
});
}
import _H from "highland";
import { stringify } from "csv-stringify";
import { createWriteStream } from "node:fs";
/**
* @param {string} filePath
* @param {unknown[][]} rows
*/
export async function writeCsvRows(filePath, rows) {
console.log(`Writing ${filePath}`);
const stream = _H(rows).pipe(stringify()).pipe(createWriteStream(filePath));
return await new Promise((resolve, reject) => {
stream.on("close", resolve);
stream.on("error", reject);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment