Class CsvParseStream<T>

CsvParseStream transforms a stream of CSV-encoded text into a stream of parsed objects.

A CsvParseStream expects input conforming to RFC 4180.

import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";
import { assertType, IsExact } from "@std/testing/types"

const source = ReadableStream.from([
"name,age\n",
"Alice,34\n",
"Bob,24\n",
]);
const stream = source.pipeThrough(new CsvParseStream());
const result = await Array.fromAsync(stream);

assertEquals(result, [
["name", "age"],
["Alice", "34"],
["Bob", "24"],
]);
assertType<IsExact<typeof result, string[][]>>(true);
import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";
import { assertType, IsExact } from "@std/testing/types"

const source = ReadableStream.from([
"Alice,34\n",
"Bob,24\n",
]);
const stream = source.pipeThrough(new CsvParseStream({
columns: ["name", "age"]
}));
const result = await Array.fromAsync(stream);

assertEquals(result, [
{ name: "Alice", age: "34" },
{ name: "Bob", age: "24" },
]);
assertType<IsExact<typeof result, Record<"name" | "age", string>[]>>(true);
import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";

const source = ReadableStream.from([
"Alice\t34\n",
"Bob\t24\n",
]);
const stream = source.pipeThrough(new CsvParseStream({
separator: "\t",
}));
const result = await Array.fromAsync(stream);

assertEquals(result, [
["Alice", "34"],
["Bob", "24"],
]);
import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";

const source = ReadableStream.from([
" Alice,34\n ",
"Bob, 24\n",
]);
const stream = source.pipeThrough(new CsvParseStream({
trimLeadingSpace: true,
}));
const result = await Array.fromAsync(stream);

assertEquals(result, [
["Alice", "34"],
["Bob", "24"],
]);
import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";

const source = ReadableStream.from([
`"a ""word""","com`,
`ma,","newline`,
`\n"\nfoo,bar,b`,
`az\n`,
]);
const stream = source.pipeThrough(new CsvParseStream());
const result = await Array.fromAsync(stream);

assertEquals(result, [
['a "word"', "comma,", "newline\n"],
["foo", "bar", "baz"]
]);
import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";

const source = ReadableStream.from([
`a "word","1"`,
`2",a","b`,
]);
const stream = source.pipeThrough(new CsvParseStream({
lazyQuotes: true,
}));
const result = await Array.fromAsync(stream);

assertEquals(result, [['a "word"', '1"2', 'a"', 'b']]);
import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";

const source = ReadableStream.from([
"Alice,34\n",
"# THIS IS A COMMENT\n",
"Bob,24\n",
]);
const stream = source.pipeThrough(new CsvParseStream({
comment: "#",
}));
const result = await Array.fromAsync(stream);

assertEquals(result, [
["Alice", "34"],
["Bob", "24"],
]);

fieldsPerRecord: 0

import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";
import { assertRejects } from "@std/assert/rejects";

const source = ReadableStream.from([
"Alice,34\n",
"Bob,24,CA\n", // Note that this row has more fields than the first row
]);
const stream = source.pipeThrough(new CsvParseStream({
fieldsPerRecord: 0,
}));
const reader = stream.getReader();
assertEquals(await reader.read(), { done: false, value: ["Alice", "34"] });
await assertRejects(
() => reader.read(),
SyntaxError,
"Syntax error on line 2: expected 2 fields but got 3",
);
import { CsvParseStream } from "@std/csv/parse-stream";
import { assertEquals } from "@std/assert/equals";
import { assertRejects } from "@std/assert/rejects";

const source = ReadableStream.from([
"Alice,34\n",
"Bob,24,CA\n",
]);
const stream = source.pipeThrough(new CsvParseStream({
fieldsPerRecord: 2,
}));
const reader = stream.getReader();
assertEquals(await reader.read(), { done: false, value: ["Alice", "34"] });
await assertRejects(
() => reader.read(),
SyntaxError,
"Syntax error on line 2: expected 2 fields but got 3",
);

Type Parameters

Implements

Constructors

Accessors

Constructors

Accessors

  • get readable(): ReadableStream<RowType<T>>
  • The instance's ReadableStream.

    Returns ReadableStream<RowType<T>>

    The instance's ReadableStream.

    import { CsvParseStream } from "@std/csv/parse-stream";
    import { assertEquals } from "@std/assert/equals";

    const source = ReadableStream.from([
    "name,age\n",
    "Alice,34\n",
    "Bob,24\n",
    ]);
    const parseStream = new CsvParseStream({ skipFirstRow: true });
    const parts = source.pipeTo(parseStream.writable);
    assertEquals(await Array.fromAsync(parseStream.readable), [
    { name: "Alice", age: "34" },
    { name: "Bob", age: "24" },
    ]);
  • get writable(): WritableStream<string>
  • The instance's WritableStream.

    Returns WritableStream<string>

    The instance's WritableStream.

    import { CsvParseStream } from "@std/csv/parse-stream";
    import { assertEquals } from "@std/assert/equals";

    const source = ReadableStream.from([
    "name,age\n",
    "Alice,34\n",
    "Bob,24\n",
    ]);
    const parseStream = new CsvParseStream({ skipFirstRow: true });
    const parts = source.pipeTo(parseStream.writable);
    assertEquals(await Array.fromAsync(parseStream.readable), [
    { name: "Alice", age: "34" },
    { name: "Bob", age: "24" },
    ]);