CSVParse 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 parser is built to iterate over the parsed records.

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

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

(async () => {
  // Initialise the parser by generating random records
  const parser = generate({
    high_water_mark: 64 * 64,
    length: 100
  }).pipe(
    parse()
  );
  // Intialise count
  let count = 0;
  // Report start
  process.stdout.write('start\n');
  // Iterate through each records
  for await (const record of parser) {
    // Report current line
    process.stdout.write(`${count++} ${record.join(',')}\n`);
    // Fake asynchronous operation
    await new Promise((resolve) => setTimeout(resolve, 100));
  }
  // Report end
  process.stdout.write('...done\n');
  // Validation
  assert.strictEqual(count, 100);
})();

About

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