Skip to content

utils‐with‐parser

Eugene Lazutkin edited this page Jun 3, 2026 · 3 revisions

utils/with-parser

withParser is a CSV-specific utility that composes the parser with another component into a single pipeline. It is used internally by as-objects to provide .withParser() and .withParserAsStream().

Import

import withParser from 'stream-csv-as-json/utils/with-parser.js';

For Web Streams, import from the browser-safe /web entry:

import withParser from 'stream-csv-as-json/web/utils/with-parser.js';

Usage

withParser is typically not used directly — instead, use asObjects.withParser():

import chain from 'stream-chain';
import asObjects from 'stream-csv-as-json/as-objects.js';
import fs from 'node:fs';

const pipeline = chain([fs.createReadStream('data.csv'), asObjects.withParser()]);

pipeline.on('data', token => console.log(token));

For custom components, withParser composes a parser and your function into a pipeline:

import withParser from 'stream-csv-as-json/utils/with-parser.js';

const myComponent = options => chunk => {
  // process tokens
  return chunk;
};

// As a flushable function for chain()
const pipeline = withParser(myComponent, options);

// As a Duplex stream (writable: text, readable: object mode)
const stream = withParser.asStream(myComponent, options);

API

Name Signature Returns Description
withParser (fn, options) flushable function Composes parser(options) with fn(options) via gen()
withParser.asStream (fn, options) Node Duplex stream Same pipeline, wrapped as a Node Duplex
withParser.asWebStream (fn, options) Web {readable, writable} pair Same pipeline, wrapped as a Web TransformStream
  • fn — a factory function (options) => flushable that creates the downstream component.
  • options — passed to both the parser and fn.

Clone this wiki locally