CSVStringify for Node.js

IssuesGitHub

Async iterator API

The Async iterator API is both scalable and elegant. It takes advantage of the native Readable Stream API upon which the stringifier is build to iterate over the stringified chunks of data.

The async iterator example below generates a CSV stream which is then stringified and iterated over. For each record, we simulate a slow asynchronous operation. This example is available with the command node samples/api.async.iterator.js.

import assert from 'node:assert';
import { generate } from "csv-generate";
import { stringify } from "csv-stringify";

(async () => {
  // Initialise the parser by generating random records
  const stringifier = generate({
    length: 1000,
    objectMode: true,
    seed: true,
  }).pipe(stringify());
  // Count records
  let count = 0;
  // Report start
  process.stdout.write("start...\n");
  // Iterate through each records
  for await (const row of stringifier) {
    // Report current line
    process.stdout.write(`${count++} ${row}\n`);
    // Fake asynchronous operation
    await new Promise((resolve) => setTimeout(resolve, 100));
  }
  // Report end
  process.stdout.write("...done\n");
  // Validation
  assert.strictEqual(count, 6);
})();

About

The Node.js CSV project is an open source product hosted on GitHub and developed by Adaltas.