-
-
Notifications
You must be signed in to change notification settings - Fork 2
parser
The parser is the core of the package. It consumes CSV text and produces a stream of {name, value} tokens using a SAX-inspired protocol compatible with stream-json.
parser(options) is a factory function returning a flushable function for use in stream-chain pipelines. It is composed with fixUtf8Stream() to handle split multi-byte characters at chunk boundaries.
// ESM
import parser from 'stream-csv-as-json/parser.js';
// CommonJS
const parser = require('stream-csv-as-json/parser.js');Or via the main module:
// ESM
import {parser} from 'stream-csv-as-json';
// CommonJS
const {parser} = require('stream-csv-as-json');As a function in a chain() pipeline:
import chain from 'stream-chain';
import fs from 'node:fs';
const pipeline = chain([fs.createReadStream('data.csv'), parser()]);
pipeline.on('data', token => console.log(token.name, token.value));As a Duplex stream:
import fs from 'node:fs';
fs.createReadStream('data.csv')
.pipe(parser.asStream())
.on('data', token => console.log(token.name, token.value));All options are optional.
| Option | Type | Default | Description |
|---|---|---|---|
packStrings |
boolean | true |
Emit stringValue tokens with the complete field value |
packValues |
boolean | — | Alias for packStrings
|
streamStrings |
boolean | true |
Emit startString/stringChunk/endString tokens |
streamValues |
boolean | — | Alias for streamStrings
|
separator |
string | ',' |
Field separator character |
If packStrings is false, streamStrings is forced to true — at least one representation must be emitted.
Both packValues/packStrings and streamValues/streamStrings follow the same pattern: the generic alias (packValues, streamValues) is applied first, then the specific option (packStrings, streamStrings) overrides it.
| Supplied options | packStrings |
streamStrings |
|---|---|---|
{} |
true |
true |
{packValues: false} |
false |
true |
{packValues: false, packStrings: true} |
true |
true |
{packStrings: true, streamStrings: false} |
true |
false |
{packStrings: false, streamStrings: false} |
false |
true |
Each row is an array of string values:
startArray
startString → stringChunk* → endString → stringValue?
startString → stringChunk* → endString → stringValue?
...
endArray
Three configurations are supported:
-
Default (
packStrings: true, streamStrings: true):startString, 0+stringChunk,endString, thenstringValue. -
Pack only (
packStrings: true, streamStrings: false):stringValueonly. -
Stream only (
packStrings: false):startString, 0+stringChunk,endStringonly.
- Follows RFC 4180.
- Quoted fields:
""escapes to", embedded separators and newlines are preserved. - Both
\r\nand\nline endings are supported. - Uses sticky RegExp (
/yflag) for performance.
| Name | Returns | Description |
|---|---|---|
parser(options) |
flushable function | For use in chain()
|
parser.asStream(options) |
Duplex stream | Writable: text, Readable: object mode |
parser.parser |
self-reference | For destructuring: const {parser} = require(...)
|
Start here
Components
Tuning
Reference
stream-csv-as-json 1.x (legacy)
Built on stream-chain and stream-json