2024-01-01 21:11:32 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-04-05 05:15:23 +00:00
|
|
|
// This module is browser compatible.
|
|
|
|
|
|
|
|
import {
|
|
|
|
convertRowToObject,
|
|
|
|
defaultReadOptions,
|
|
|
|
type LineReader,
|
|
|
|
parseRecord,
|
|
|
|
type ParseResult,
|
2024-04-29 02:57:30 +00:00
|
|
|
} from "./_io.ts";
|
|
|
|
import { TextDelimiterStream } from "@std/streams/text-delimiter-stream";
|
2023-04-05 05:15:23 +00:00
|
|
|
|
2024-01-11 06:02:30 +00:00
|
|
|
/** Options for {@linkcode CsvParseStream}. */
|
2024-06-27 07:26:41 +00:00
|
|
|
export interface CsvParseStreamOptions {
|
|
|
|
/** Character which separates values.
|
|
|
|
*
|
|
|
|
* @default {","}
|
|
|
|
*/
|
|
|
|
separator?: string;
|
|
|
|
/** Character to start a comment.
|
|
|
|
*
|
|
|
|
* Lines beginning with the comment character without preceding whitespace
|
|
|
|
* are ignored. With leading whitespace the comment character becomes part of
|
|
|
|
* the field, even you provide `trimLeadingSpace: true`.
|
|
|
|
*
|
2024-08-02 04:24:15 +00:00
|
|
|
* By default, no character is considered to be a start of a comment.
|
2024-06-27 07:26:41 +00:00
|
|
|
*/
|
|
|
|
comment?: string;
|
|
|
|
/** Flag to trim the leading space of the value.
|
|
|
|
*
|
|
|
|
* This is done even if the field delimiter, `separator`, is white space.
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
|
|
|
trimLeadingSpace?: boolean;
|
|
|
|
/**
|
|
|
|
* Allow unquoted quote in a quoted field or non-double-quoted quotes in
|
|
|
|
* quoted field.
|
|
|
|
*
|
|
|
|
* @default {false}
|
|
|
|
*/
|
|
|
|
lazyQuotes?: boolean;
|
|
|
|
/**
|
|
|
|
* Enabling checking number of expected fields for each row.
|
|
|
|
*
|
|
|
|
* If positive, each record is required to have the given number of fields.
|
|
|
|
* If 0, it will be set to the number of fields in the first row, so that
|
|
|
|
* future rows must have the same field count.
|
|
|
|
* If negative, no check is made and records may have a variable number of
|
|
|
|
* fields.
|
|
|
|
*
|
2024-08-02 04:24:15 +00:00
|
|
|
* If the wrong number of fields is in a row, a {@linkcode https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError | SyntaxError}
|
|
|
|
* is thrown.
|
2024-06-27 07:26:41 +00:00
|
|
|
*/
|
|
|
|
fieldsPerRecord?: number;
|
2023-04-05 05:15:23 +00:00
|
|
|
/**
|
|
|
|
* If you provide `skipFirstRow: true` and `columns`, the first line will be
|
|
|
|
* skipped.
|
|
|
|
* If you provide `skipFirstRow: true` but not `columns`, the first line will
|
|
|
|
* be skipped and used as header definitions.
|
2024-07-11 09:21:37 +00:00
|
|
|
*
|
|
|
|
* @default {false}
|
2023-04-05 05:15:23 +00:00
|
|
|
*/
|
|
|
|
skipFirstRow?: boolean;
|
|
|
|
/** List of names used for header definition. */
|
|
|
|
columns?: readonly string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
class StreamLineReader implements LineReader {
|
|
|
|
#reader: ReadableStreamDefaultReader<string>;
|
|
|
|
#done = false;
|
|
|
|
constructor(reader: ReadableStreamDefaultReader<string>) {
|
|
|
|
this.#reader = reader;
|
|
|
|
}
|
|
|
|
|
|
|
|
async readLine(): Promise<string | null> {
|
|
|
|
const { value, done } = await this.#reader.read();
|
|
|
|
if (done) {
|
|
|
|
this.#done = true;
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
// NOTE: Remove trailing CR for compatibility with golang's `encoding/csv`
|
|
|
|
return stripLastCR(value!);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-12 02:45:25 +00:00
|
|
|
isEOF(): boolean {
|
|
|
|
return this.#done;
|
2023-04-05 05:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
this.#reader.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function stripLastCR(s: string): string {
|
|
|
|
return s.endsWith("\r") ? s.slice(0, -1) : s;
|
|
|
|
}
|
|
|
|
|
2024-01-11 06:02:30 +00:00
|
|
|
/** Row return type. */
|
|
|
|
export type RowType<T> = T extends undefined ? string[]
|
2023-04-05 05:15:23 +00:00
|
|
|
: ParseResult<CsvParseStreamOptions, T>[number];
|
|
|
|
|
|
|
|
/**
|
2024-08-02 04:07:15 +00:00
|
|
|
* `CsvParseStream` transforms a stream of CSV-encoded text into a stream of
|
|
|
|
* parsed objects.
|
2023-04-05 05:15:23 +00:00
|
|
|
*
|
|
|
|
* A `CsvParseStream` expects input conforming to
|
2024-05-20 07:14:09 +00:00
|
|
|
* {@link https://www.rfc-editor.org/rfc/rfc4180.html | RFC 4180}.
|
2023-04-05 05:15:23 +00:00
|
|
|
*
|
2024-08-02 04:24:15 +00:00
|
|
|
* @example Usage with default options
|
|
|
|
* ```ts
|
2024-06-28 06:07:36 +00:00
|
|
|
* import { CsvParseStream } from "@std/csv/parse-stream";
|
2024-08-02 04:24:15 +00:00
|
|
|
* import { assertEquals } from "@std/assert/equals";
|
|
|
|
* import { assertType, IsExact } from "@std/testing/types"
|
2024-06-03 03:32:09 +00:00
|
|
|
*
|
|
|
|
* const source = ReadableStream.from([
|
2024-08-02 04:24:15 +00:00
|
|
|
* "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);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Skip first row with `skipFirstRow: true`
|
|
|
|
* ```ts
|
|
|
|
* 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({ skipFirstRow: true }));
|
|
|
|
* const result = await Array.fromAsync(stream);
|
|
|
|
*
|
|
|
|
* assertEquals(result, [
|
|
|
|
* { name: "Alice", age: "34" },
|
|
|
|
* { name: "Bob", age: "24" },
|
|
|
|
* ]);
|
2024-08-02 05:07:10 +00:00
|
|
|
* assertType<IsExact<typeof result, Record<string, string>[]>>(true);
|
2024-08-02 04:24:15 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Specify columns with `columns` option
|
|
|
|
* ```ts
|
|
|
|
* 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);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Specify columns with `columns` option and skip first row with
|
|
|
|
* `skipFirstRow: true`
|
|
|
|
* ```ts
|
|
|
|
* 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"],
|
|
|
|
* skipFirstRow: true,
|
|
|
|
* }));
|
|
|
|
* const result = await Array.fromAsync(stream);
|
|
|
|
*
|
|
|
|
* assertEquals(result, [{ name: "Bob", age: "24" }]);
|
|
|
|
* assertType<IsExact<typeof result, Record<"name" | "age", string>[]>>(true);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example TSV (tab-separated values) with `separator: "\t"`
|
|
|
|
* ```ts
|
|
|
|
* 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"],
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Trim leading space with `trimLeadingSpace: true`
|
|
|
|
* ```ts
|
|
|
|
* 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"],
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Quoted fields
|
|
|
|
* ```ts
|
|
|
|
* 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"]
|
2024-06-03 03:32:09 +00:00
|
|
|
* ]);
|
2024-08-02 04:24:15 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Allow lazy quotes with `lazyQuotes: true`
|
|
|
|
* ```ts
|
|
|
|
* 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']]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Define comment prefix with `comment` option
|
|
|
|
* ```ts
|
|
|
|
* 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"],
|
|
|
|
* ]);
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Infer the number of fields from the first row with
|
|
|
|
* `fieldsPerRecord: 0`
|
|
|
|
* ```ts
|
|
|
|
* 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,
|
|
|
|
* "record on line 2: expected 2 fields but got 3",
|
|
|
|
* );
|
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @example Enforce the number of field for each row with `fieldsPerRecord: 2`
|
|
|
|
* ```ts
|
|
|
|
* 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,
|
|
|
|
* "record on line 2: expected 2 fields but got 3",
|
|
|
|
* );
|
2023-04-05 05:15:23 +00:00
|
|
|
* ```
|
2024-06-03 03:32:09 +00:00
|
|
|
*
|
|
|
|
* @typeParam T The type of options for the stream.
|
2023-04-05 05:15:23 +00:00
|
|
|
*/
|
|
|
|
export class CsvParseStream<
|
|
|
|
const T extends CsvParseStreamOptions | undefined = undefined,
|
|
|
|
> implements TransformStream<string, RowType<T>> {
|
|
|
|
readonly #readable: ReadableStream<
|
|
|
|
string[] | Record<string, string | unknown>
|
|
|
|
>;
|
|
|
|
readonly #options: CsvParseStreamOptions;
|
|
|
|
readonly #lineReader: StreamLineReader;
|
|
|
|
readonly #lines: TextDelimiterStream;
|
2024-08-01 15:07:52 +00:00
|
|
|
#zeroBasedLineIndex = 0;
|
2023-04-05 05:15:23 +00:00
|
|
|
#isFirstRow = true;
|
|
|
|
|
2024-08-01 10:48:14 +00:00
|
|
|
// The number of fields per record that is either inferred from the first row
|
|
|
|
// (when options.fieldsPerRecord = 0), or set by the caller (when
|
|
|
|
// options.fieldsPerRecord > 0).
|
|
|
|
//
|
|
|
|
// Each possible variant means the following:
|
|
|
|
// "ANY": Variable number of fields is allowed.
|
|
|
|
// "UNINITIALIZED": The first row has not been read yet. Once it's read, the
|
|
|
|
// number of fields will be set.
|
|
|
|
// <number>: The number of fields per record that every record must follow.
|
|
|
|
#fieldsPerRecord: "ANY" | "UNINITIALIZED" | number;
|
|
|
|
|
2023-04-05 05:15:23 +00:00
|
|
|
#headers: readonly string[] = [];
|
|
|
|
|
2024-06-03 03:32:09 +00:00
|
|
|
/** Construct a new instance.
|
|
|
|
*
|
|
|
|
* @param options Options for the stream.
|
|
|
|
*/
|
2023-05-12 02:45:25 +00:00
|
|
|
constructor(options?: T) {
|
2023-04-05 05:15:23 +00:00
|
|
|
this.#options = {
|
|
|
|
...defaultReadOptions,
|
|
|
|
...options,
|
|
|
|
};
|
|
|
|
|
2024-08-01 10:48:14 +00:00
|
|
|
if (
|
|
|
|
this.#options.fieldsPerRecord === undefined ||
|
|
|
|
this.#options.fieldsPerRecord < 0
|
|
|
|
) {
|
|
|
|
this.#fieldsPerRecord = "ANY";
|
|
|
|
} else if (this.#options.fieldsPerRecord === 0) {
|
|
|
|
this.#fieldsPerRecord = "UNINITIALIZED";
|
|
|
|
} else {
|
|
|
|
// TODO: Should we check if it's a valid integer?
|
|
|
|
this.#fieldsPerRecord = this.#options.fieldsPerRecord;
|
|
|
|
}
|
|
|
|
|
2023-04-05 05:15:23 +00:00
|
|
|
this.#lines = new TextDelimiterStream("\n");
|
|
|
|
this.#lineReader = new StreamLineReader(this.#lines.readable.getReader());
|
|
|
|
this.#readable = new ReadableStream({
|
|
|
|
pull: (controller) => this.#pull(controller),
|
|
|
|
cancel: () => this.#lineReader.cancel(),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async #pull(
|
|
|
|
controller: ReadableStreamDefaultController<
|
|
|
|
string[] | Record<string, string | unknown>
|
|
|
|
>,
|
|
|
|
): Promise<void> {
|
|
|
|
const line = await this.#lineReader.readLine();
|
|
|
|
if (line === "") {
|
|
|
|
// Found an empty line
|
2024-08-01 15:07:52 +00:00
|
|
|
this.#zeroBasedLineIndex++;
|
2023-04-05 05:15:23 +00:00
|
|
|
return this.#pull(controller);
|
|
|
|
}
|
|
|
|
if (line === null) {
|
|
|
|
// Reached to EOF
|
|
|
|
controller.close();
|
|
|
|
this.#lineReader.cancel();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const record = await parseRecord(
|
|
|
|
line,
|
|
|
|
this.#lineReader,
|
|
|
|
this.#options,
|
2024-08-01 15:07:52 +00:00
|
|
|
this.#zeroBasedLineIndex,
|
2023-04-05 05:15:23 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (this.#isFirstRow) {
|
|
|
|
this.#isFirstRow = false;
|
|
|
|
if (this.#options.skipFirstRow || this.#options.columns) {
|
|
|
|
this.#headers = [];
|
|
|
|
|
|
|
|
if (this.#options.skipFirstRow) {
|
|
|
|
const head = record;
|
|
|
|
this.#headers = head;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.#options.columns) {
|
|
|
|
this.#headers = this.#options.columns;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.#options.skipFirstRow) {
|
|
|
|
return this.#pull(controller);
|
|
|
|
}
|
2024-08-01 10:48:14 +00:00
|
|
|
|
|
|
|
if (this.#fieldsPerRecord === "UNINITIALIZED") {
|
|
|
|
this.#fieldsPerRecord = record.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
typeof this.#fieldsPerRecord === "number" &&
|
|
|
|
record.length !== this.#fieldsPerRecord
|
|
|
|
) {
|
|
|
|
throw new SyntaxError(
|
|
|
|
`record on line ${
|
2024-08-01 15:07:52 +00:00
|
|
|
this.#zeroBasedLineIndex + 1
|
2024-08-01 10:48:14 +00:00
|
|
|
}: expected ${this.#fieldsPerRecord} fields but got ${record.length}`,
|
|
|
|
);
|
2023-04-05 05:15:23 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 15:07:52 +00:00
|
|
|
this.#zeroBasedLineIndex++;
|
2023-04-05 05:15:23 +00:00
|
|
|
if (record.length > 0) {
|
|
|
|
if (this.#options.skipFirstRow || this.#options.columns) {
|
|
|
|
controller.enqueue(convertRowToObject(
|
|
|
|
record,
|
|
|
|
this.#headers,
|
2024-08-01 15:07:52 +00:00
|
|
|
this.#zeroBasedLineIndex,
|
2023-04-05 05:15:23 +00:00
|
|
|
));
|
|
|
|
} else {
|
|
|
|
controller.enqueue(record);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return this.#pull(controller);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-03 03:32:09 +00:00
|
|
|
/**
|
|
|
|
* The instance's {@linkcode ReadableStream}.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2024-08-02 04:24:15 +00:00
|
|
|
* ```ts
|
2024-06-28 06:07:36 +00:00
|
|
|
* import { CsvParseStream } from "@std/csv/parse-stream";
|
2024-08-02 04:24:15 +00:00
|
|
|
* import { assertEquals } from "@std/assert/equals";
|
2024-06-03 03:32:09 +00:00
|
|
|
*
|
|
|
|
* const source = ReadableStream.from([
|
2024-08-02 04:24:15 +00:00
|
|
|
* "name,age\n",
|
|
|
|
* "Alice,34\n",
|
|
|
|
* "Bob,24\n",
|
2024-06-03 03:32:09 +00:00
|
|
|
* ]);
|
2024-08-02 04:24:15 +00:00
|
|
|
* const parseStream = new CsvParseStream({ skipFirstRow: true });
|
2024-06-03 03:32:09 +00:00
|
|
|
* const parts = source.pipeTo(parseStream.writable);
|
2024-08-02 04:24:15 +00:00
|
|
|
* assertEquals(await Array.fromAsync(parseStream.readable), [
|
|
|
|
* { name: "Alice", age: "34" },
|
|
|
|
* { name: "Bob", age: "24" },
|
|
|
|
* ]);
|
2024-06-03 03:32:09 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns The instance's {@linkcode ReadableStream}.
|
|
|
|
*/
|
2023-12-19 00:26:13 +00:00
|
|
|
get readable(): ReadableStream<RowType<T>> {
|
2023-04-05 05:15:23 +00:00
|
|
|
return this.#readable as ReadableStream<RowType<T>>;
|
|
|
|
}
|
|
|
|
|
2024-06-03 03:32:09 +00:00
|
|
|
/**
|
|
|
|
* The instance's {@linkcode WritableStream}.
|
|
|
|
*
|
|
|
|
* @example Usage
|
2024-08-02 04:24:15 +00:00
|
|
|
* ```ts
|
2024-06-28 06:07:36 +00:00
|
|
|
* import { CsvParseStream } from "@std/csv/parse-stream";
|
2024-08-02 04:24:15 +00:00
|
|
|
* import { assertEquals } from "@std/assert/equals";
|
2024-06-03 03:32:09 +00:00
|
|
|
*
|
|
|
|
* const source = ReadableStream.from([
|
2024-08-02 04:24:15 +00:00
|
|
|
* "name,age\n",
|
|
|
|
* "Alice,34\n",
|
|
|
|
* "Bob,24\n",
|
2024-06-03 03:32:09 +00:00
|
|
|
* ]);
|
2024-08-02 04:24:15 +00:00
|
|
|
* const parseStream = new CsvParseStream({ skipFirstRow: true });
|
2024-06-03 03:32:09 +00:00
|
|
|
* const parts = source.pipeTo(parseStream.writable);
|
2024-08-02 04:24:15 +00:00
|
|
|
* assertEquals(await Array.fromAsync(parseStream.readable), [
|
|
|
|
* { name: "Alice", age: "34" },
|
|
|
|
* { name: "Bob", age: "24" },
|
|
|
|
* ]);
|
2024-06-03 03:32:09 +00:00
|
|
|
* ```
|
|
|
|
*
|
|
|
|
* @returns The instance's {@linkcode WritableStream}.
|
|
|
|
*/
|
2023-04-05 05:15:23 +00:00
|
|
|
get writable(): WritableStream<string> {
|
|
|
|
return this.#lines.writable;
|
|
|
|
}
|
|
|
|
}
|