web-csv-toolbox 0.4.0 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -9
- package/dist/web-csv-toolbox.cjs +2 -0
- package/dist/web-csv-toolbox.cjs.map +1 -0
- package/dist/web-csv-toolbox.d.ts +982 -0
- package/dist/web-csv-toolbox.js +576 -0
- package/dist/web-csv-toolbox.js.map +1 -0
- package/dist/web-csv-toolbox.umd.cjs +2 -0
- package/dist/web-csv-toolbox.umd.cjs.map +1 -0
- package/package.json +18 -17
- package/lib/index.d.ts +0 -952
- package/lib/index.js +0 -430
- package/lib/index.umd.js +0 -1
package/lib/index.js
DELETED
|
@@ -1,430 +0,0 @@
|
|
|
1
|
-
const FieldDelimiter = Symbol.for("web-csv-toolbox.FieldDelimiter");
|
|
2
|
-
const RecordDelimiter = Symbol.for("web-csv-toolbox.RecordDelimiter");
|
|
3
|
-
const Field = Symbol.for("web-csv-toolbox.Field");
|
|
4
|
-
const CR = "\r";
|
|
5
|
-
const CRLF = "\r\n";
|
|
6
|
-
const LF = "\n";
|
|
7
|
-
const COMMA = ",";
|
|
8
|
-
const DOUBLE_QUATE = '"';
|
|
9
|
-
function assertCommonOptions(options) {
|
|
10
|
-
if (typeof options.quotation === "string" && options.quotation.length === 0) {
|
|
11
|
-
throw new Error("quotation must not be empty");
|
|
12
|
-
}
|
|
13
|
-
if (typeof options.demiliter === "string" && options.demiliter.length === 0) {
|
|
14
|
-
throw new Error("demiliter must not be empty");
|
|
15
|
-
}
|
|
16
|
-
if (options.quotation.includes(LF) || options.quotation.includes(CR)) {
|
|
17
|
-
throw new Error("quotation must not include CR or LF");
|
|
18
|
-
}
|
|
19
|
-
if (options.demiliter.includes(LF) || options.demiliter.includes(CR)) {
|
|
20
|
-
throw new Error("demiliter must not include CR or LF");
|
|
21
|
-
}
|
|
22
|
-
if (
|
|
23
|
-
options.demiliter.includes(options.quotation) ||
|
|
24
|
-
options.quotation.includes(options.demiliter)
|
|
25
|
-
) {
|
|
26
|
-
throw new Error(
|
|
27
|
-
"demiliter and quotation must not include each other as a substring",
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
function escapeRegExp(v) {
|
|
32
|
-
return v.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
33
|
-
}
|
|
34
|
-
class LexerTransformer extends TransformStream {
|
|
35
|
-
#demiliter;
|
|
36
|
-
#demiliterLength;
|
|
37
|
-
#quotation;
|
|
38
|
-
#quotationLength;
|
|
39
|
-
#matcher;
|
|
40
|
-
#buffer = "";
|
|
41
|
-
get demiliter() {
|
|
42
|
-
return this.#demiliter;
|
|
43
|
-
}
|
|
44
|
-
get quotation() {
|
|
45
|
-
return this.#quotation;
|
|
46
|
-
}
|
|
47
|
-
constructor({ demiliter = COMMA, quotation = DOUBLE_QUATE } = {}) {
|
|
48
|
-
assertCommonOptions({ demiliter, quotation });
|
|
49
|
-
super({
|
|
50
|
-
transform: (chunk, controller) => {
|
|
51
|
-
if (chunk.length !== 0) {
|
|
52
|
-
this.#buffer += chunk;
|
|
53
|
-
for (const token of this.#tokens({ flush: false })) {
|
|
54
|
-
controller.enqueue(token);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
},
|
|
58
|
-
flush: (controller) => {
|
|
59
|
-
for (const token of this.#tokens({ flush: true })) {
|
|
60
|
-
controller.enqueue(token);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
});
|
|
64
|
-
this.#demiliter = demiliter;
|
|
65
|
-
this.#demiliterLength = demiliter.length;
|
|
66
|
-
this.#quotation = quotation;
|
|
67
|
-
this.#quotationLength = quotation.length;
|
|
68
|
-
const d = escapeRegExp(demiliter);
|
|
69
|
-
const q = escapeRegExp(quotation);
|
|
70
|
-
this.#matcher = new RegExp(
|
|
71
|
-
`^(?:(?!${q})(?!${d})(?![\\r\\n]))([\\S\\s\\uFEFF\\xA0]+?)(?=${q}|${d}|\\r|\\n|$)`,
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
*#tokens({ flush }) {
|
|
75
|
-
let currentField = null;
|
|
76
|
-
for (let token; (token = this.#nextToken({ flush })); ) {
|
|
77
|
-
switch (token.type) {
|
|
78
|
-
case Field:
|
|
79
|
-
if (currentField) {
|
|
80
|
-
currentField.value += token.value;
|
|
81
|
-
} else {
|
|
82
|
-
currentField = token;
|
|
83
|
-
}
|
|
84
|
-
break;
|
|
85
|
-
case FieldDelimiter:
|
|
86
|
-
if (currentField) {
|
|
87
|
-
yield currentField;
|
|
88
|
-
currentField = null;
|
|
89
|
-
}
|
|
90
|
-
yield token;
|
|
91
|
-
break;
|
|
92
|
-
case RecordDelimiter:
|
|
93
|
-
if (currentField) {
|
|
94
|
-
yield currentField;
|
|
95
|
-
currentField = null;
|
|
96
|
-
}
|
|
97
|
-
yield token;
|
|
98
|
-
break;
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
if (currentField) {
|
|
102
|
-
yield currentField;
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
#nextToken({ flush = false } = {}) {
|
|
106
|
-
if (this.#buffer.length === 0) {
|
|
107
|
-
return null;
|
|
108
|
-
}
|
|
109
|
-
if (this.#buffer.startsWith(CRLF)) {
|
|
110
|
-
this.#buffer = this.#buffer.slice(2);
|
|
111
|
-
return { type: RecordDelimiter, value: CRLF };
|
|
112
|
-
}
|
|
113
|
-
if (this.#buffer.startsWith(LF)) {
|
|
114
|
-
this.#buffer = this.#buffer.slice(1);
|
|
115
|
-
return { type: RecordDelimiter, value: LF };
|
|
116
|
-
}
|
|
117
|
-
if (this.#buffer.startsWith(this.#demiliter)) {
|
|
118
|
-
this.#buffer = this.#buffer.slice(this.#demiliterLength);
|
|
119
|
-
return { type: FieldDelimiter, value: this.#demiliter };
|
|
120
|
-
}
|
|
121
|
-
if (this.#buffer.startsWith(this.#quotation)) {
|
|
122
|
-
if (flush === false && this.#buffer.endsWith(this.#quotation)) {
|
|
123
|
-
return null;
|
|
124
|
-
}
|
|
125
|
-
return this.extractQuotedString(flush);
|
|
126
|
-
}
|
|
127
|
-
const match = this.#matcher.exec(this.#buffer);
|
|
128
|
-
if (match) {
|
|
129
|
-
if (flush === false && match[0].length === this.#buffer.length) {
|
|
130
|
-
return null;
|
|
131
|
-
}
|
|
132
|
-
this.#buffer = this.#buffer.slice(match[0].length);
|
|
133
|
-
return { type: Field, value: match[0] };
|
|
134
|
-
}
|
|
135
|
-
return null;
|
|
136
|
-
}
|
|
137
|
-
extractQuotedString(flush) {
|
|
138
|
-
let end = this.#quotationLength;
|
|
139
|
-
let value = "";
|
|
140
|
-
while (end < this.#buffer.length) {
|
|
141
|
-
if (
|
|
142
|
-
this.#buffer.slice(end, end + this.#quotationLength) ===
|
|
143
|
-
this.quotation &&
|
|
144
|
-
this.#buffer.slice(
|
|
145
|
-
end + this.#quotationLength,
|
|
146
|
-
end + this.#quotationLength * 2,
|
|
147
|
-
) === this.quotation
|
|
148
|
-
) {
|
|
149
|
-
value += this.quotation;
|
|
150
|
-
end += this.#quotationLength * 2;
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
if (
|
|
154
|
-
this.#buffer.slice(end, end + this.#quotationLength) === this.quotation
|
|
155
|
-
) {
|
|
156
|
-
if (
|
|
157
|
-
flush === false &&
|
|
158
|
-
end + this.#quotationLength < this.#buffer.length &&
|
|
159
|
-
this.#buffer.slice(
|
|
160
|
-
end + this.#quotationLength,
|
|
161
|
-
this.#demiliterLength,
|
|
162
|
-
) !== this.demiliter &&
|
|
163
|
-
this.#buffer.slice(
|
|
164
|
-
end + this.#quotationLength,
|
|
165
|
-
end + this.#quotationLength + 2,
|
|
166
|
-
) !== CRLF &&
|
|
167
|
-
this.#buffer.slice(
|
|
168
|
-
end + this.#quotationLength,
|
|
169
|
-
end + this.#quotationLength + 1,
|
|
170
|
-
) !== LF
|
|
171
|
-
) {
|
|
172
|
-
return null;
|
|
173
|
-
}
|
|
174
|
-
this.#buffer = this.#buffer.slice(end + this.#quotationLength);
|
|
175
|
-
return { type: Field, value };
|
|
176
|
-
}
|
|
177
|
-
value += this.#buffer[end];
|
|
178
|
-
end++;
|
|
179
|
-
}
|
|
180
|
-
return null;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
class RecordAssemblerTransformar extends TransformStream {
|
|
184
|
-
#fieldIndex = 0;
|
|
185
|
-
#row = [];
|
|
186
|
-
#header;
|
|
187
|
-
#darty = false;
|
|
188
|
-
constructor(options = {}) {
|
|
189
|
-
super({
|
|
190
|
-
transform: (token, controller) => {
|
|
191
|
-
switch (token.type) {
|
|
192
|
-
case Field:
|
|
193
|
-
this.#darty = true;
|
|
194
|
-
this.#row[this.#fieldIndex] = token.value;
|
|
195
|
-
break;
|
|
196
|
-
case FieldDelimiter:
|
|
197
|
-
this.#fieldIndex++;
|
|
198
|
-
break;
|
|
199
|
-
case RecordDelimiter:
|
|
200
|
-
if (this.#header === undefined) {
|
|
201
|
-
this.#setHeader(this.#row);
|
|
202
|
-
} else {
|
|
203
|
-
if (this.#darty) {
|
|
204
|
-
const record = Object.fromEntries(
|
|
205
|
-
this.#header
|
|
206
|
-
.filter((v) => v)
|
|
207
|
-
.map((header, index) => [header, this.#row.at(index)]),
|
|
208
|
-
);
|
|
209
|
-
controller.enqueue(record);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
this.#fieldIndex = 0;
|
|
213
|
-
this.#row = new Array(this.#header?.length);
|
|
214
|
-
this.#darty = false;
|
|
215
|
-
break;
|
|
216
|
-
}
|
|
217
|
-
},
|
|
218
|
-
flush: (controller) => {
|
|
219
|
-
if (this.#fieldIndex !== 0 && this.#header !== undefined) {
|
|
220
|
-
if (this.#darty) {
|
|
221
|
-
const record = Object.fromEntries(
|
|
222
|
-
this.#header
|
|
223
|
-
.filter((v) => v)
|
|
224
|
-
.map((header, index) => [header, this.#row.at(index)]),
|
|
225
|
-
);
|
|
226
|
-
controller.enqueue(record);
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
},
|
|
230
|
-
});
|
|
231
|
-
if (options.header !== undefined && Array.isArray(options.header)) {
|
|
232
|
-
this.#setHeader(options.header);
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
#setHeader(header) {
|
|
236
|
-
this.#header = header;
|
|
237
|
-
if (this.#header.length === 0) {
|
|
238
|
-
throw new Error("The header must not be empty.");
|
|
239
|
-
}
|
|
240
|
-
if (new Set(this.#header).size !== this.#header.length) {
|
|
241
|
-
throw new Error("The header must not contain duplicate fields.");
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
class SingleValueReadableStream extends ReadableStream {
|
|
246
|
-
constructor(value) {
|
|
247
|
-
super({
|
|
248
|
-
start(controller) {
|
|
249
|
-
controller.enqueue(value);
|
|
250
|
-
controller.close();
|
|
251
|
-
},
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
}
|
|
255
|
-
async function toArray(...args) {
|
|
256
|
-
const rows = [];
|
|
257
|
-
for await (const row of this(...args)) {
|
|
258
|
-
rows.push(row);
|
|
259
|
-
}
|
|
260
|
-
return rows;
|
|
261
|
-
}
|
|
262
|
-
async function* parseStringStream(stream, options) {
|
|
263
|
-
let controller;
|
|
264
|
-
const readable = new ReadableStream({
|
|
265
|
-
start: (controller_) => (controller = controller_),
|
|
266
|
-
});
|
|
267
|
-
await stream
|
|
268
|
-
.pipeThrough(new LexerTransformer(options))
|
|
269
|
-
.pipeThrough(new RecordAssemblerTransformar(options))
|
|
270
|
-
.pipeTo(
|
|
271
|
-
new WritableStream({
|
|
272
|
-
write: (row) => controller.enqueue(row),
|
|
273
|
-
close: () => controller.close(),
|
|
274
|
-
}),
|
|
275
|
-
);
|
|
276
|
-
const reader = readable.getReader();
|
|
277
|
-
try {
|
|
278
|
-
while (true) {
|
|
279
|
-
const { value, done } = await reader.read();
|
|
280
|
-
if (done) break;
|
|
281
|
-
yield value;
|
|
282
|
-
}
|
|
283
|
-
} finally {
|
|
284
|
-
reader.releaseLock();
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
((parseStringStream) => {
|
|
288
|
-
Object.defineProperty(parseStringStream, "toArray", {
|
|
289
|
-
enumerable: true,
|
|
290
|
-
writable: false,
|
|
291
|
-
value: toArray,
|
|
292
|
-
});
|
|
293
|
-
})(parseStringStream || (parseStringStream = {}));
|
|
294
|
-
async function* parseString(csv, options) {
|
|
295
|
-
yield* parseStringStream(new SingleValueReadableStream(csv), options);
|
|
296
|
-
}
|
|
297
|
-
((parseString) => {
|
|
298
|
-
Object.defineProperty(parseString, "toArray", {
|
|
299
|
-
enumerable: true,
|
|
300
|
-
writable: false,
|
|
301
|
-
value: toArray,
|
|
302
|
-
});
|
|
303
|
-
})(parseString || (parseString = {}));
|
|
304
|
-
async function* parseUint8ArrayStream(stream, options) {
|
|
305
|
-
const { charset, fatal, ignoreBOM, decomposition } = options ?? {};
|
|
306
|
-
yield* parseStringStream(
|
|
307
|
-
[
|
|
308
|
-
...(decomposition ? [new DecompressionStream(decomposition)] : []),
|
|
309
|
-
new TextDecoderStream(charset, { fatal, ignoreBOM }),
|
|
310
|
-
].reduce((stream, transformer) => stream.pipeThrough(transformer), stream),
|
|
311
|
-
options,
|
|
312
|
-
);
|
|
313
|
-
}
|
|
314
|
-
((parseUint8ArrayStream) => {
|
|
315
|
-
Object.defineProperty(parseUint8ArrayStream, "toArray", {
|
|
316
|
-
enumerable: true,
|
|
317
|
-
writable: false,
|
|
318
|
-
value: toArray,
|
|
319
|
-
});
|
|
320
|
-
})(parseUint8ArrayStream || (parseUint8ArrayStream = {}));
|
|
321
|
-
function parseUint8Array(bytes, options) {
|
|
322
|
-
return parseUint8ArrayStream(new SingleValueReadableStream(bytes), options);
|
|
323
|
-
}
|
|
324
|
-
((parseUint8Array) => {
|
|
325
|
-
Object.defineProperty(parseUint8Array, "toArray", {
|
|
326
|
-
enumerable: true,
|
|
327
|
-
writable: false,
|
|
328
|
-
value: toArray,
|
|
329
|
-
});
|
|
330
|
-
})(parseUint8Array || (parseUint8Array = {}));
|
|
331
|
-
function parseArrayBuffer(buffer, options) {
|
|
332
|
-
return parseUint8Array(new Uint8Array(buffer), options);
|
|
333
|
-
}
|
|
334
|
-
((parseArrayBuffer) => {
|
|
335
|
-
Object.defineProperty(parseArrayBuffer, "toArray", {
|
|
336
|
-
enumerable: true,
|
|
337
|
-
writable: false,
|
|
338
|
-
value: toArray,
|
|
339
|
-
});
|
|
340
|
-
})(parseArrayBuffer || (parseArrayBuffer = {}));
|
|
341
|
-
function parseMime(contentType) {
|
|
342
|
-
const [type, ...parameters] = contentType.split(";");
|
|
343
|
-
const result = {
|
|
344
|
-
type: type.trim(),
|
|
345
|
-
parameters: {},
|
|
346
|
-
};
|
|
347
|
-
for (const paramator of parameters) {
|
|
348
|
-
const [key, value] = paramator.split("=");
|
|
349
|
-
result.parameters[key.trim()] = value.trim();
|
|
350
|
-
}
|
|
351
|
-
return result;
|
|
352
|
-
}
|
|
353
|
-
function parseResponse(response, options) {
|
|
354
|
-
const { headers } = response;
|
|
355
|
-
const contentType = headers.get("content-type") ?? "text/csv";
|
|
356
|
-
const mime = parseMime(contentType);
|
|
357
|
-
if (mime.type !== "text/csv") {
|
|
358
|
-
throw new Error(`Invalid mime type: ${contentType}`);
|
|
359
|
-
}
|
|
360
|
-
const decomposition = headers.get("content-encoding") ?? undefined;
|
|
361
|
-
const charset = mime.parameters.charset ?? "utf-8";
|
|
362
|
-
if (response.body === null) {
|
|
363
|
-
throw new Error("Response body is null");
|
|
364
|
-
}
|
|
365
|
-
return parseUint8ArrayStream(response.body, {
|
|
366
|
-
decomposition,
|
|
367
|
-
charset,
|
|
368
|
-
...options,
|
|
369
|
-
});
|
|
370
|
-
}
|
|
371
|
-
((parseResponse) => {
|
|
372
|
-
Object.defineProperty(parseResponse, "toArray", {
|
|
373
|
-
enumerable: true,
|
|
374
|
-
writable: false,
|
|
375
|
-
value: toArray,
|
|
376
|
-
});
|
|
377
|
-
})(parseResponse || (parseResponse = {}));
|
|
378
|
-
async function* parseStream(stream, options) {
|
|
379
|
-
const [branch1, branch2] = stream.tee();
|
|
380
|
-
const reader1 = branch1.getReader();
|
|
381
|
-
const { value: firstChunk } = await reader1.read();
|
|
382
|
-
reader1.releaseLock();
|
|
383
|
-
if (typeof firstChunk === "string") {
|
|
384
|
-
yield* parseStringStream(branch2, options);
|
|
385
|
-
} else if (firstChunk instanceof Uint8Array) {
|
|
386
|
-
yield* parseUint8ArrayStream(branch2, options);
|
|
387
|
-
}
|
|
388
|
-
}
|
|
389
|
-
((parseStream) => {
|
|
390
|
-
Object.defineProperty(parseStream, "toArray", {
|
|
391
|
-
enumerable: true,
|
|
392
|
-
writable: false,
|
|
393
|
-
value: toArray,
|
|
394
|
-
});
|
|
395
|
-
})(parseStream || (parseStream = {}));
|
|
396
|
-
async function* parse(csv, options) {
|
|
397
|
-
if (typeof csv === "string") {
|
|
398
|
-
yield* parseString(csv, options);
|
|
399
|
-
} else if (csv instanceof Uint8Array) {
|
|
400
|
-
yield* parseUint8Array(csv, options);
|
|
401
|
-
} else if (csv instanceof ArrayBuffer) {
|
|
402
|
-
yield* parseArrayBuffer(csv, options);
|
|
403
|
-
} else if (csv instanceof ReadableStream) {
|
|
404
|
-
yield* parseStream(csv, options);
|
|
405
|
-
} else if (csv instanceof Response) {
|
|
406
|
-
yield* parseResponse(csv, options);
|
|
407
|
-
}
|
|
408
|
-
}
|
|
409
|
-
((parse) => {
|
|
410
|
-
Object.defineProperty(parse, "toArray", {
|
|
411
|
-
enumerable: true,
|
|
412
|
-
writable: false,
|
|
413
|
-
value: toArray,
|
|
414
|
-
});
|
|
415
|
-
})(parse || (parse = {}));
|
|
416
|
-
export {
|
|
417
|
-
Field,
|
|
418
|
-
FieldDelimiter,
|
|
419
|
-
LexerTransformer,
|
|
420
|
-
RecordAssemblerTransformar,
|
|
421
|
-
RecordDelimiter,
|
|
422
|
-
parse,
|
|
423
|
-
parseArrayBuffer,
|
|
424
|
-
parseResponse,
|
|
425
|
-
parseStream,
|
|
426
|
-
parseString,
|
|
427
|
-
parseStringStream,
|
|
428
|
-
parseUint8Array,
|
|
429
|
-
parseUint8ArrayStream,
|
|
430
|
-
};
|
package/lib/index.umd.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CSV={})}(this,(function(e){"use strict";const t=Symbol.for("web-csv-toolbox.FieldDelimiter"),i=Symbol.for("web-csv-toolbox.RecordDelimiter"),r=Symbol.for("web-csv-toolbox.Field"),n="\r\n",o="\n";function s(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}class a extends TransformStream{#e;#t;#i;#r;#n;#o="";get demiliter(){return this.#e}get quotation(){return this.#i}constructor({demiliter:e=",",quotation:t='"'}={}){!function(e){if("string"==typeof e.quotation&&0===e.quotation.length)throw new Error("quotation must not be empty");if("string"==typeof e.demiliter&&0===e.demiliter.length)throw new Error("demiliter must not be empty");if(e.quotation.includes(o)||e.quotation.includes("\r"))throw new Error("quotation must not include CR or LF");if(e.demiliter.includes(o)||e.demiliter.includes("\r"))throw new Error("demiliter must not include CR or LF");if(e.demiliter.includes(e.quotation)||e.quotation.includes(e.demiliter))throw new Error("demiliter and quotation must not include each other as a substring")}({demiliter:e,quotation:t}),super({transform:(e,t)=>{if(0!==e.length){this.#o+=e;for(const e of this.#s({flush:!1}))t.enqueue(e)}},flush:e=>{for(const t of this.#s({flush:!0}))e.enqueue(t)}}),this.#e=e,this.#t=e.length,this.#i=t,this.#r=t.length;const i=s(e),r=s(t);this.#n=new RegExp(`^(?:(?!${r})(?!${i})(?![\\r\\n]))([\\S\\s\\uFEFF\\xA0]+?)(?=${r}|${i}|\\r|\\n|$)`)}*#s({flush:e}){let n=null;for(let o;o=this.#a({flush:e});)switch(o.type){case r:n?n.value+=o.value:n=o;break;case t:case i:n&&(yield n,n=null),yield o}n&&(yield n)}#a({flush:e=!1}={}){if(0===this.#o.length)return null;if(this.#o.startsWith(n))return this.#o=this.#o.slice(2),{type:i,value:n};if(this.#o.startsWith(o))return this.#o=this.#o.slice(1),{type:i,value:o};if(this.#o.startsWith(this.#e))return this.#o=this.#o.slice(this.#t),{type:t,value:this.#e};if(this.#o.startsWith(this.#i))return!1===e&&this.#o.endsWith(this.#i)?null:this.extractQuotedString(e);const s=this.#n.exec(this.#o);return s?!1===e&&s[0].length===this.#o.length?null:(this.#o=this.#o.slice(s[0].length),{type:r,value:s[0]}):null}extractQuotedString(e){let t=this.#r,i="";for(;t<this.#o.length;)if(this.#o.slice(t,t+this.#r)!==this.quotation||this.#o.slice(t+this.#r,t+2*this.#r)!==this.quotation){if(this.#o.slice(t,t+this.#r)===this.quotation)return!1===e&&t+this.#r<this.#o.length&&this.#o.slice(t+this.#r,this.#t)!==this.demiliter&&this.#o.slice(t+this.#r,t+this.#r+2)!==n&&this.#o.slice(t+this.#r,t+this.#r+1)!==o?null:(this.#o=this.#o.slice(t+this.#r),{type:r,value:i});i+=this.#o[t],t++}else i+=this.quotation,t+=2*this.#r;return null}}class u extends TransformStream{#u=0;#l=[];#h;#f=!1;constructor(e={}){super({transform:(e,n)=>{switch(e.type){case r:this.#f=!0,this.#l[this.#u]=e.value;break;case t:this.#u++;break;case i:if(void 0===this.#h)this.#c(this.#l);else if(this.#f){const e=Object.fromEntries(this.#h.filter((e=>e)).map(((e,t)=>[e,this.#l.at(t)])));n.enqueue(e)}this.#u=0,this.#l=new Array(this.#h?.length),this.#f=!1}},flush:e=>{if(0!==this.#u&&void 0!==this.#h&&this.#f){const t=Object.fromEntries(this.#h.filter((e=>e)).map(((e,t)=>[e,this.#l.at(t)])));e.enqueue(t)}}}),void 0!==e.header&&Array.isArray(e.header)&&this.#c(e.header)}#c(e){if(this.#h=e,0===this.#h.length)throw new Error("The header must not be empty.");if(new Set(this.#h).size!==this.#h.length)throw new Error("The header must not contain duplicate fields.")}}class l extends ReadableStream{constructor(e){super({start(t){t.enqueue(e),t.close()}})}}async function h(...e){const t=[];for await(const i of this(...e))t.push(i);return t}async function*f(e,t){let i;const r=new ReadableStream({start:e=>i=e});await e.pipeThrough(new a(t)).pipeThrough(new u(t)).pipeTo(new WritableStream({write:e=>i.enqueue(e),close:()=>i.close()}));const n=r.getReader();try{for(;;){const{value:e,done:t}=await n.read();if(t)break;yield e}}finally{n.releaseLock()}}async function*c(e,t){yield*f(new l(e),t)}async function*d(e,t){const{charset:i,fatal:r,ignoreBOM:n,decomposition:o}=t??{};yield*f([...o?[new DecompressionStream(o)]:[],new TextDecoderStream(i,{fatal:r,ignoreBOM:n})].reduce(((e,t)=>e.pipeThrough(t)),e),t)}function m(e,t){return d(new l(e),t)}function b(e,t){return m(new Uint8Array(e),t)}function y(e,t){const{headers:i}=e,r=i.get("content-type")??"text/csv",n=function(e){const[t,...i]=e.split(";"),r={type:t.trim(),parameters:{}};for(const e of i){const[t,i]=e.split("=");r.parameters[t.trim()]=i.trim()}return r}(r);if("text/csv"!==n.type)throw new Error(`Invalid mime type: ${r}`);const o=i.get("content-encoding")??void 0,s=n.parameters.charset??"utf-8";if(null===e.body)throw new Error("Response body is null");return d(e.body,{decomposition:o,charset:s,...t})}async function*p(e,t){const[i,r]=e.tee(),n=i.getReader(),{value:o}=await n.read();n.releaseLock(),"string"==typeof o?yield*f(r,t):o instanceof Uint8Array&&(yield*d(r,t))}async function*g(e,t){"string"==typeof e?yield*c(e,t):e instanceof Uint8Array?yield*m(e,t):e instanceof ArrayBuffer?yield*b(e,t):e instanceof ReadableStream?yield*p(e,t):e instanceof Response&&(yield*y(e,t))}!function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:h})}(f||(f={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:h})}(c||(c={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:h})}(d||(d={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:h})}(m||(m={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:h})}(b||(b={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:h})}(y||(y={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:h})}(p||(p={})),function(e){Object.defineProperty(e,"toArray",{enumerable:!0,writable:!1,value:h})}(g||(g={})),e.Field=r,e.FieldDelimiter=t,e.LexerTransformer=a,e.RecordAssemblerTransformar=u,e.RecordDelimiter=i,e.parse=g,e.parseArrayBuffer=b,e.parseResponse=y,e.parseStream=p,e.parseString=c,e.parseStringStream=f,e.parseUint8Array=m,e.parseUint8ArrayStream=d}));
|