-
Notifications
You must be signed in to change notification settings - Fork 641
/
Copy pathstringify_stream.ts
139 lines (135 loc) · 3.64 KB
/
stringify_stream.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 2018-2025 the Deno authors. MIT license.
// This module is browser compatible.
import { stringify } from "./stringify.ts";
/** Options for {@linkcode CsvStringifyStream}. */
export interface CsvStringifyStreamOptions {
/**
* Delimiter used to separate values.
*
* @default {","}
*/
readonly separator?: string;
/**
* A list of columns to be included in the output.
*
* If you want to stream objects, this option is required.
*
* @default {[]}
*/
readonly columns?: Array<string>;
}
/**
* Convert each chunk to a CSV record.
*
* @example Write CSV to a file
* ```ts
* import { CsvStringifyStream } from "@std/csv/stringify-stream";
* import { assertEquals } from "@std/assert/equals";
*
* async function writeCsvToTempFile(): Promise<string> {
* const path = await Deno.makeTempFile();
* using file = await Deno.open(path, { write: true });
*
* const readable = ReadableStream.from([
* { id: 1, name: "one" },
* { id: 2, name: "two" },
* { id: 3, name: "three" },
* ]);
*
* await readable
* .pipeThrough(new CsvStringifyStream({ columns: ["id", "name"] }))
* .pipeThrough(new TextEncoderStream())
* .pipeTo(file.writable);
*
* return path;
* }
*
* const path = await writeCsvToTempFile();
* const content = await Deno.readTextFile(path);
* assertEquals(content, "id,name\r\n1,one\r\n2,two\r\n3,three\r\n");
* ```
*
* @example Write TSV to a file
* ```ts
* import { CsvStringifyStream } from "@std/csv/stringify-stream";
* import { assertEquals } from "@std/assert/equals";
*
* async function writeTsvToTempFile(): Promise<string> {
* const path = await Deno.makeTempFile();
* using file = await Deno.open(path, { write: true });
*
* const readable = ReadableStream.from([
* { id: 1, name: "one" },
* { id: 2, name: "two" },
* { id: 3, name: "three" },
* ]);
*
* await readable
* .pipeThrough(
* new CsvStringifyStream({
* columns: ["id", "name"],
* separator: "\t",
* }),
* )
* .pipeThrough(new TextEncoderStream())
* .pipeTo(file.writable);
*
* return path;
* }
*
* const path = await writeTsvToTempFile();
* const content = await Deno.readTextFile(path);
* assertEquals(content, "id\tname\r\n1\tone\r\n2\ttwo\r\n3\tthree\r\n");
* ```
*
* @typeParam TOptions The type of options for the stream.
*/
export class CsvStringifyStream<TOptions extends CsvStringifyStreamOptions>
extends TransformStream<
TOptions["columns"] extends Array<string> ? Record<string, unknown>
: Array<unknown>,
string
> {
/**
* Construct a new instance.
*
* @param options Options for the stream.
*/
constructor(options?: TOptions) {
const { separator, columns = [] } = options ?? {};
super(
{
start(controller) {
if (columns && columns.length > 0) {
try {
controller.enqueue(
stringify(
[columns],
separator !== undefined
? { separator, headers: false }
: { headers: false },
),
);
} catch (error) {
controller.error(error);
}
}
},
transform(chunk, controller) {
try {
controller.enqueue(
stringify(
[chunk],
separator !== undefined
? { separator, headers: false, columns }
: { headers: false, columns },
),
);
} catch (error) {
controller.error(error);
}
},
},
);
}
}