-
Notifications
You must be signed in to change notification settings - Fork 5
/
writer.ts
219 lines (189 loc) · 5.56 KB
/
writer.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { concat } from "@std/bytes/concat";
import { indexOfNeedle } from "@std/bytes/index-of-needle";
import type { SyncAsyncIterable } from "./utils.ts";
import {
dummyAsyncIterable,
getUint8Array,
hasPrefixFrom,
isAsyncIterable,
makeAsyncIterable,
} from "./utils.ts";
/** Options for CSV writer */
export interface CSVWriterOptions {
columnSeparator: string | Uint8Array;
lineSeparator: string | Uint8Array;
quote: string | Uint8Array;
}
/** Options for `CSVWriter.writeCell` */
export interface CSVWriteCellOptions {
forceQuotes: boolean;
}
const defaultCSVWriterOptions = {
columnSeparator: ",",
lineSeparator: "\n",
quote: '"',
};
/** Class for manual CSV writing:
*
* const writer = new CSVWriter(f, {
* columnSeparator: "\t",
* lineSeparator: "\r\n",
* });
* await writer.writeCell("a\nb");
* await writer.nextLine();
* await writer.writeCell('1"2');
*/
export class CSVWriter {
private writer: Deno.Writer;
private columnSeparator: Uint8Array;
private lineSeparator: Uint8Array;
private quote: Uint8Array;
private firstColumn: boolean;
constructor(writer: Deno.Writer, options?: Partial<CSVWriterOptions>) {
this.writer = writer;
this.columnSeparator = getUint8Array(
(options && options.columnSeparator) ||
defaultCSVWriterOptions.columnSeparator,
);
this.lineSeparator = getUint8Array(
(options && options.lineSeparator) ||
defaultCSVWriterOptions.lineSeparator,
);
this.quote = getUint8Array(
(options && options.quote) || defaultCSVWriterOptions.quote,
);
this.firstColumn = true;
}
public writeCell(
str: string | Uint8Array | AsyncIterable<Uint8Array>,
options?: Partial<CSVWriteCellOptions>,
): Promise<void> {
if (isAsyncIterable(str)) {
return this._writeCellAsyncIterable(str, { wrap: true });
}
const arr = getUint8Array(str);
const wrap = options?.forceQuotes ||
indexOfNeedle(arr, this.quote) >= 0 ||
indexOfNeedle(arr, this.columnSeparator) >= 0 ||
indexOfNeedle(arr, this.lineSeparator) >= 0;
return this._writeCellAsyncIterable(dummyAsyncIterable(arr), {
wrap,
});
}
private async _writeCellAsyncIterable(
iterable: AsyncIterable<Uint8Array>,
options: { wrap: boolean },
): Promise<void> {
const { quote } = this;
const { wrap } = options;
const iterator = iterable[Symbol.asyncIterator]();
let inputBuffer = new Uint8Array();
let inputBufferEmpty = false;
let inputBufferIndex = 0;
if (this.firstColumn) {
this.firstColumn = false;
} else {
await this.writer.write(this.columnSeparator);
}
if (wrap) {
await this.writer.write(this.quote);
}
while (true) {
const inputBufferUnprocessed = inputBuffer.length - inputBufferIndex;
if (inputBufferEmpty && inputBufferUnprocessed === 0) {
break;
}
if (!inputBufferEmpty && inputBufferUnprocessed < quote.length) {
const { done, value } = await iterator.next();
if (done) {
inputBufferEmpty = true;
} else {
inputBuffer = concat([inputBuffer, value]);
}
continue;
}
if (wrap && hasPrefixFrom(inputBuffer, quote, inputBufferIndex)) {
await this.writer.write(quote);
await this.writer.write(quote);
inputBufferIndex += quote.length;
continue;
}
if (inputBufferUnprocessed > 0) {
await this.writer.write(
inputBuffer.subarray(inputBufferIndex, inputBufferIndex + 1),
);
inputBufferIndex++;
continue;
}
throw new Error("unexpected");
}
if (wrap) {
await this.writer.write(this.quote);
}
}
public async nextLine() {
this.firstColumn = true;
await this.writer.write(this.lineSeparator);
}
}
/** Write CSV with sync or async row iterators:
*
* await writeCSV(f, [["a", "b"], ["1", "2"]]);
*
* const asyncRowGenerator = async function*() {
* yield ["a", "b"];
* yield ["1", "2"];
* }
* await writeCSV(f, asyncRowGenerator());
*/
export async function writeCSV(
writer: Deno.Writer,
iter: SyncAsyncIterable<
SyncAsyncIterable<string | Uint8Array | AsyncIterable<Uint8Array>>
>,
options?: Partial<CSVWriterOptions & CSVWriteCellOptions>,
) {
const csv = new CSVWriter(writer, options);
let firstLine = true;
for await (const row of makeAsyncIterable(iter)) {
if (firstLine) {
firstLine = false;
} else {
await csv.nextLine();
}
for await (const cell of makeAsyncIterable(row)) {
await csv.writeCell(cell, options);
}
}
}
/** Write CSV with sync or async object iterators:
*
* await writeCSVObjects(f, [{a: "1"}, {a: "2"}], { header: ["a"] });
*
* const asyncObjectsGenerator = async function*() {
* yield { a: "1", b: "2", c: "3" };
* yield { a: "4", b: "5", c: "6" };
* }
* await writeCSVObjects(f, asyncObjectsGenerator(), { header: ["a", "b", "c"] });
*/
export async function writeCSVObjects(
writer: Deno.Writer,
iter: SyncAsyncIterable<{ [key: string]: string }>,
options: Partial<CSVWriterOptions & CSVWriteCellOptions> & {
header: string[];
},
) {
const { header } = options;
const row = function* (obj: { [key: string]: string }) {
for (const key of header) {
yield obj[key];
}
};
const rows = async function* () {
yield header;
for await (const obj of makeAsyncIterable(iter)) {
yield row(obj);
}
};
await writeCSV(writer, rows(), options);
}