web-csv-toolbox 0.1.0 → 0.3.0
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 +117 -64
- package/lib/index.d.ts +496 -23
- package/lib/index.js +33 -216
- package/lib/index.umd.js +1 -0
- package/package.json +7 -4
package/lib/index.d.ts
CHANGED
|
@@ -1,19 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* FiledDelimiter is a symbol for field delimiter of CSV.
|
|
3
|
+
* @category Constants
|
|
3
4
|
*/
|
|
4
5
|
declare const FieldDelimiter: unique symbol;
|
|
5
6
|
/**
|
|
6
7
|
* RecordDelimiter is a symbol for record delimiter of CSV.
|
|
8
|
+
* @category Constants
|
|
7
9
|
*/
|
|
8
10
|
declare const RecordDelimiter: unique symbol;
|
|
9
11
|
/**
|
|
10
12
|
* Field is a symbol for field of CSV.
|
|
13
|
+
* @category Constants
|
|
11
14
|
*/
|
|
12
15
|
declare const Field: unique symbol;
|
|
13
16
|
|
|
14
17
|
/**
|
|
15
18
|
* Token is a atomic unit of a CSV file.
|
|
16
19
|
* It can be a field, field delimiter, or record delimiter.
|
|
20
|
+
* @category Types
|
|
17
21
|
*
|
|
18
22
|
* @example
|
|
19
23
|
* ```ts
|
|
@@ -28,10 +32,12 @@ interface Token<T extends TokenType = TokenType> {
|
|
|
28
32
|
}
|
|
29
33
|
/**
|
|
30
34
|
* Type of a token for CSV.
|
|
35
|
+
* @category Types
|
|
31
36
|
*/
|
|
32
37
|
type TokenType = typeof FieldDelimiter | typeof RecordDelimiter | typeof Field;
|
|
33
38
|
/**
|
|
34
39
|
* CSV Common Options.
|
|
40
|
+
* @category Types
|
|
35
41
|
*/
|
|
36
42
|
interface CommonOptions {
|
|
37
43
|
/**
|
|
@@ -56,6 +62,7 @@ interface CommonOptions {
|
|
|
56
62
|
}
|
|
57
63
|
/**
|
|
58
64
|
* CSV Parsing Options for binary.
|
|
65
|
+
* @category Types
|
|
59
66
|
*/
|
|
60
67
|
interface BinaryOptions {
|
|
61
68
|
/**
|
|
@@ -64,6 +71,7 @@ interface BinaryOptions {
|
|
|
64
71
|
*
|
|
65
72
|
* @remarks
|
|
66
73
|
* Make sure the runtime you are running supports stream decompression.
|
|
74
|
+
*
|
|
67
75
|
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/DecompressionStream#browser_compatibility | DecompressionStream Compatibility}.
|
|
68
76
|
*/
|
|
69
77
|
decomposition?: CompressionFormat;
|
|
@@ -71,7 +79,8 @@ interface BinaryOptions {
|
|
|
71
79
|
* You can specify the character encoding of the binary.
|
|
72
80
|
*
|
|
73
81
|
* @remarks
|
|
74
|
-
* {@link TextDecoderStream} is used internally.
|
|
82
|
+
* {@link !TextDecoderStream} is used internally.
|
|
83
|
+
*
|
|
75
84
|
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings | Encoding API Compatibility}
|
|
76
85
|
* for the encoding formats that can be specified.
|
|
77
86
|
*
|
|
@@ -92,14 +101,21 @@ interface BinaryOptions {
|
|
|
92
101
|
* If the binary has a invalid character, you can specify whether to throw an error.
|
|
93
102
|
*
|
|
94
103
|
* @remarks
|
|
95
|
-
* If
|
|
96
|
-
*
|
|
97
|
-
*
|
|
104
|
+
* If the property is `true` then a decoder will throw a {@link !TypeError}
|
|
105
|
+
* if it encounters malformed data while decoding.
|
|
106
|
+
*
|
|
107
|
+
* If `false` the decoder will substitute the invalid data
|
|
108
|
+
* with the replacement character `U+FFFD` (�).
|
|
109
|
+
*
|
|
110
|
+
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream/fatal | TextDecoderOptions.fatal} for more information.
|
|
111
|
+
*
|
|
112
|
+
* @default false
|
|
98
113
|
*/
|
|
99
114
|
fatal?: boolean;
|
|
100
115
|
}
|
|
101
116
|
/**
|
|
102
117
|
* Record Assembler Options for CSV.
|
|
118
|
+
* @category Types
|
|
103
119
|
*
|
|
104
120
|
* @remarks
|
|
105
121
|
* If you specify `header: ['foo', 'bar']`,
|
|
@@ -125,18 +141,21 @@ interface RecordAssemblerOptions<Header extends ReadonlyArray<string>> {
|
|
|
125
141
|
}
|
|
126
142
|
/**
|
|
127
143
|
* Parse options for CSV string.
|
|
144
|
+
* @category Types
|
|
128
145
|
*/
|
|
129
146
|
interface ParseOptions<Header extends ReadonlyArray<string>>
|
|
130
147
|
extends CommonOptions,
|
|
131
148
|
RecordAssemblerOptions<Header> {}
|
|
132
149
|
/**
|
|
133
150
|
* Parse options for CSV binary.
|
|
151
|
+
* @category Types
|
|
134
152
|
*/
|
|
135
153
|
interface ParseBinaryOptions<Header extends ReadonlyArray<string>>
|
|
136
154
|
extends ParseOptions<Header>,
|
|
137
155
|
BinaryOptions {}
|
|
138
156
|
/**
|
|
139
157
|
* CSV Record.
|
|
158
|
+
* @category Types
|
|
140
159
|
* @template Header Header of the CSV.
|
|
141
160
|
*
|
|
142
161
|
* @example Header is ["foo", "bar"]
|
|
@@ -155,6 +174,8 @@ type CSVRecord<Header extends ReadonlyArray<string>> = Record<
|
|
|
155
174
|
/**
|
|
156
175
|
* A transform stream that converts a stream of tokens into a stream of rows.
|
|
157
176
|
*
|
|
177
|
+
* @category Low-level API
|
|
178
|
+
*
|
|
158
179
|
* @example Parse a CSV with headers by data
|
|
159
180
|
* ```ts
|
|
160
181
|
* new ReadableStream({
|
|
@@ -189,6 +210,8 @@ declare class LexerTransformer extends TransformStream<string, Token> {
|
|
|
189
210
|
* @template Header The type of the header row.
|
|
190
211
|
* @param options The options for the parser.
|
|
191
212
|
*
|
|
213
|
+
* @category Low-level API
|
|
214
|
+
*
|
|
192
215
|
* @example Parse a CSV with headers by data
|
|
193
216
|
* ```ts
|
|
194
217
|
* new ReadableStream({
|
|
@@ -235,16 +258,55 @@ declare class RecordAssemblerTransformar<
|
|
|
235
258
|
/**
|
|
236
259
|
* Parse CSV string to records.
|
|
237
260
|
*
|
|
261
|
+
* @category Middle-level API
|
|
238
262
|
* @param csv CSV string to parse
|
|
239
263
|
* @param options Parsing options. See {@link ParseOptions}.
|
|
264
|
+
* @returns Async iterable iterator of records.
|
|
265
|
+
*
|
|
266
|
+
* If you want array of records, use {@link parseString.toArray} function.
|
|
267
|
+
* @example Parsing CSV files from strings
|
|
268
|
+
*
|
|
269
|
+
* ```ts
|
|
270
|
+
* import { parseString } from 'web-csv-toolbox';
|
|
271
|
+
*
|
|
272
|
+
* const csv = `name,age
|
|
273
|
+
* Alice,42
|
|
274
|
+
* Bob,69`;
|
|
275
|
+
*
|
|
276
|
+
* for await (const record of parseString(csv)) {
|
|
277
|
+
* console.log(record);
|
|
278
|
+
* }
|
|
279
|
+
* // Prints:
|
|
280
|
+
* // { name: 'Alice', age: '42' }
|
|
281
|
+
* // { name: 'Bob', age: '69' }
|
|
282
|
+
* ```
|
|
240
283
|
*/
|
|
241
|
-
declare function
|
|
284
|
+
declare function parseString<Header extends ReadonlyArray<string>>(
|
|
242
285
|
csv: string,
|
|
243
286
|
options?: ParseOptions<Header>,
|
|
244
287
|
): AsyncIterableIterator<CSVRecord<Header>>;
|
|
245
|
-
declare namespace
|
|
288
|
+
declare namespace parseString {
|
|
289
|
+
/**
|
|
290
|
+
* Parse CSV string to records.
|
|
291
|
+
*
|
|
292
|
+
* @returns Array of records
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```ts
|
|
296
|
+
* import { parseString } from 'web-csv-toolbox';
|
|
297
|
+
*
|
|
298
|
+
* const csv = `name,age
|
|
299
|
+
* Alice,42
|
|
300
|
+
* Bob,69`;
|
|
301
|
+
*
|
|
302
|
+
* const records = await parseString.toArray(csv);
|
|
303
|
+
* console.log(records);
|
|
304
|
+
* // Prints:
|
|
305
|
+
* // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
|
|
306
|
+
* ```
|
|
307
|
+
*/
|
|
246
308
|
function toArray<Header extends ReadonlyArray<string>>(
|
|
247
|
-
|
|
309
|
+
csv: string,
|
|
248
310
|
options?: ParseOptions<Header>,
|
|
249
311
|
): Promise<CSVRecord<Header>[]>;
|
|
250
312
|
}
|
|
@@ -253,16 +315,66 @@ declare namespace streamingParse {
|
|
|
253
315
|
* Parse CSV to records.
|
|
254
316
|
* This function is for parsing a binary stream.
|
|
255
317
|
*
|
|
318
|
+
* @category Middle-level API
|
|
256
319
|
* @remarks
|
|
257
|
-
* If you want to parse a string, use {@link
|
|
320
|
+
* If you want to parse a string, use {@link parseStringStream}.
|
|
258
321
|
* @param stream CSV string to parse
|
|
259
322
|
* @param options Parsing options. See {@link ParseBinaryOptions}.
|
|
323
|
+
* @returns Async iterable iterator of records.
|
|
324
|
+
*
|
|
325
|
+
* If you want array of records, use {@link parseBinaryStream.toArray} function.
|
|
326
|
+
*
|
|
327
|
+
* @example Parsing CSV binary
|
|
328
|
+
*
|
|
329
|
+
* ```ts
|
|
330
|
+
* import { parseBinaryStream } from 'web-csv-toolbox';
|
|
331
|
+
*
|
|
332
|
+
* const csv = Uint8Array.from([
|
|
333
|
+
* // ...
|
|
334
|
+
* ]);
|
|
335
|
+
*
|
|
336
|
+
* const stream = new ReadableStream({
|
|
337
|
+
* start(controller) {
|
|
338
|
+
* controller.enqueue(csv);
|
|
339
|
+
* controller.close();
|
|
340
|
+
* },
|
|
341
|
+
* });
|
|
342
|
+
*
|
|
343
|
+
* for await (const record of parseBinaryStream(csv)) {
|
|
344
|
+
* console.log(record);
|
|
345
|
+
* }
|
|
346
|
+
* ```
|
|
260
347
|
*/
|
|
261
348
|
declare function parseBinaryStream<Header extends ReadonlyArray<string>>(
|
|
262
349
|
stream: ReadableStream<Uint8Array>,
|
|
263
350
|
options?: ParseBinaryOptions<Header>,
|
|
264
351
|
): AsyncIterableIterator<CSVRecord<Header>>;
|
|
265
352
|
declare namespace parseBinaryStream {
|
|
353
|
+
/**
|
|
354
|
+
* Parse CSV binary to array of records,
|
|
355
|
+
* ideal for smaller data sets.
|
|
356
|
+
*
|
|
357
|
+
* @returns Array of records
|
|
358
|
+
*
|
|
359
|
+
* @example Parsing CSV binary
|
|
360
|
+
* ```ts
|
|
361
|
+
* import { parseBinaryStream } from 'web-csv-toolbox';
|
|
362
|
+
*
|
|
363
|
+
* const csv = Uint8Array.from([
|
|
364
|
+
* // ...
|
|
365
|
+
* ]);
|
|
366
|
+
*
|
|
367
|
+
* const stream = new ReadableStream({
|
|
368
|
+
* start(controller) {
|
|
369
|
+
* controller.enqueue(csv);
|
|
370
|
+
* controller.close();
|
|
371
|
+
* },
|
|
372
|
+
* });
|
|
373
|
+
*
|
|
374
|
+
* const records = await parseBinaryStream.toArray(stream);
|
|
375
|
+
* console.log(records);
|
|
376
|
+
* ```
|
|
377
|
+
*/
|
|
266
378
|
function toArray<Header extends ReadonlyArray<string>>(
|
|
267
379
|
stream: ReadableStream<Uint8Array>,
|
|
268
380
|
options?: ParseBinaryOptions<Header>,
|
|
@@ -270,48 +382,407 @@ declare namespace parseBinaryStream {
|
|
|
270
382
|
}
|
|
271
383
|
|
|
272
384
|
/**
|
|
273
|
-
* Parse CSV string to records.
|
|
385
|
+
* Parse CSV string stream to records.
|
|
274
386
|
*
|
|
387
|
+
* @category Middle-level API
|
|
275
388
|
* @param stream CSV string stream to parse
|
|
276
|
-
* @param options Parsing options.
|
|
389
|
+
* @param options Parsing options.
|
|
390
|
+
* @returns Async iterable iterator of records.
|
|
391
|
+
*
|
|
392
|
+
* If you want array of records, use {@link parseStringStream.toArray} function.
|
|
393
|
+
*
|
|
394
|
+
* @example Parsing CSV files from strings
|
|
395
|
+
*
|
|
396
|
+
* ```ts
|
|
397
|
+
* import { parseStringStream } from 'web-csv-toolbox';
|
|
398
|
+
*
|
|
399
|
+
* const csv = `name,age
|
|
400
|
+
* Alice,42
|
|
401
|
+
* Bob,69`;
|
|
402
|
+
*
|
|
403
|
+
* const stream = new ReadableStream({
|
|
404
|
+
* start(controller) {
|
|
405
|
+
* controller.enqueue(csv);
|
|
406
|
+
* controller.close();
|
|
407
|
+
* },
|
|
408
|
+
* });
|
|
409
|
+
*
|
|
410
|
+
* for await (const record of parseStringStream(csv)) {
|
|
411
|
+
* console.log(record);
|
|
412
|
+
* }
|
|
413
|
+
* // Prints:
|
|
414
|
+
* // { name: 'Alice', age: '42' }
|
|
415
|
+
* // { name: 'Bob', age: '69' }
|
|
416
|
+
* ```
|
|
277
417
|
*/
|
|
278
418
|
declare function parseStringStream<Header extends ReadonlyArray<string>>(
|
|
279
419
|
stream: ReadableStream<string>,
|
|
280
420
|
options?: ParseOptions<Header>,
|
|
281
421
|
): AsyncIterableIterator<CSVRecord<Header>>;
|
|
282
422
|
declare namespace parseStringStream {
|
|
423
|
+
/**
|
|
424
|
+
* Parse CSV string stream to records.
|
|
425
|
+
*
|
|
426
|
+
* @returns Array of records
|
|
427
|
+
*
|
|
428
|
+
* @example
|
|
429
|
+
*
|
|
430
|
+
* ```ts
|
|
431
|
+
* import { parseStringStream } from 'web-csv-toolbox';
|
|
432
|
+
*
|
|
433
|
+
* const csv = `name,age
|
|
434
|
+
* Alice,42
|
|
435
|
+
* Bob,69`;
|
|
436
|
+
*
|
|
437
|
+
* const stream = new ReadableStream({
|
|
438
|
+
* start(controller) {
|
|
439
|
+
* controller.enqueue(csv);
|
|
440
|
+
* controller.close();
|
|
441
|
+
* },
|
|
442
|
+
* });
|
|
443
|
+
*
|
|
444
|
+
* const records = await parseStringStream.toArray(stream);
|
|
445
|
+
* console.log(records);
|
|
446
|
+
* // Prints:
|
|
447
|
+
* // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
|
|
448
|
+
* ```
|
|
449
|
+
*/
|
|
283
450
|
function toArray<Header extends ReadonlyArray<string>>(
|
|
284
|
-
stream: ReadableStream<
|
|
451
|
+
stream: ReadableStream<string>,
|
|
285
452
|
options?: ParseOptions<Header>,
|
|
286
453
|
): Promise<CSVRecord<Header>[]>;
|
|
287
454
|
}
|
|
288
455
|
|
|
289
456
|
/**
|
|
290
|
-
* Parse CSV to records
|
|
457
|
+
* Parse HTTP Response what contains CSV to records,
|
|
458
|
+
* ideal for smaller data sets.
|
|
459
|
+
*
|
|
460
|
+
* @remarks
|
|
461
|
+
* This function automatically treats response headers.
|
|
291
462
|
*
|
|
292
|
-
*
|
|
463
|
+
* - If `Content-Type` header is not set, it assumes `text/csv`.
|
|
464
|
+
* - If `Content-Type` header is not `text/csv`, it throws an error.
|
|
465
|
+
* - If `Content-Type` header has charset parameter, it uses it for decoding.
|
|
466
|
+
* - If `Content-Encoding` header is set, it decompresses the response.
|
|
467
|
+
* - Should there be any conflicting information between the header and the options, the option's value will take precedence.
|
|
468
|
+
*
|
|
469
|
+
* @category Middle-level API
|
|
470
|
+
* @param response
|
|
471
|
+
* @param options
|
|
472
|
+
* @returns Async iterable iterator of records.
|
|
473
|
+
*
|
|
474
|
+
* If you want array of records, use {@link parseResponse.toArray} function.
|
|
475
|
+
*
|
|
476
|
+
* @example Parsing CSV Response
|
|
477
|
+
*
|
|
478
|
+
* ```ts
|
|
479
|
+
* import { parseResponse } from 'web-csv-toolbox';
|
|
480
|
+
*
|
|
481
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
482
|
+
*
|
|
483
|
+
* for await (const record of parseResponse(response)) {
|
|
484
|
+
* console.log(record);
|
|
485
|
+
* }
|
|
486
|
+
* ```
|
|
487
|
+
*/
|
|
488
|
+
declare function parseResponse<Header extends ReadonlyArray<string>>(
|
|
489
|
+
response: Response,
|
|
490
|
+
options?: ParseOptions<Header>,
|
|
491
|
+
): AsyncIterableIterator<CSVRecord<Header>>;
|
|
492
|
+
declare namespace parseResponse {
|
|
493
|
+
/**
|
|
494
|
+
* Parse CSV Response to array of records.
|
|
495
|
+
*
|
|
496
|
+
* @returns Array of records
|
|
497
|
+
*
|
|
498
|
+
* @example Parsing CSV Response
|
|
499
|
+
*
|
|
500
|
+
* ```ts
|
|
501
|
+
* import { parseResponse } from 'web-csv-toolbox';
|
|
502
|
+
*
|
|
503
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
504
|
+
*
|
|
505
|
+
* const records = await parseResponse.toArray(response);
|
|
506
|
+
* console.log(records);
|
|
507
|
+
* ```
|
|
508
|
+
*/
|
|
509
|
+
function toArray<Header extends ReadonlyArray<string>>(
|
|
510
|
+
response: Response,
|
|
511
|
+
options?: ParseOptions<Header>,
|
|
512
|
+
): Promise<CSVRecord<Header>[]>;
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
/**
|
|
516
|
+
* Parse CSV Stream to records,
|
|
517
|
+
* ideal for smaller data sets.
|
|
518
|
+
*
|
|
519
|
+
* {@link !ReadableStream} of {@link !String} and {@link !Uint8Array} are supported.
|
|
293
520
|
*
|
|
294
521
|
* @remarks
|
|
295
|
-
* {@link
|
|
296
|
-
* {@link parseStringStream} and {@link parseResponse} are used internally.
|
|
522
|
+
* {@link parseStringStream} and {@link parseBinaryStream} are used internally.
|
|
297
523
|
* If you known the type of the stream, it performs better to use them directly.
|
|
298
524
|
*
|
|
299
|
-
* If you want to parse a string, use {@link
|
|
300
|
-
* If you want to parse a Uint8Array, use {@link
|
|
301
|
-
* If you want to parse a ReadableStream<string>, use {@link parseStringStream}.
|
|
302
|
-
* If you want to parse a ReadableStream<Uint8Array>, use {@link parseBinaryStream}.
|
|
303
|
-
* If you want to parse a Response, use {@link parseResponse}.
|
|
525
|
+
* If you want to parse a string, use {@link parseStringStream}.
|
|
526
|
+
* If you want to parse a Uint8Array, use {@link parseBinaryStream}.
|
|
304
527
|
*
|
|
528
|
+
* @category Middle-level API
|
|
305
529
|
* @param csv CSV string to parse
|
|
306
530
|
* @param options Parsing options. See {@link ParseOptions}.
|
|
531
|
+
* @returns Async iterable iterator of records.
|
|
532
|
+
*
|
|
533
|
+
* If you want array of records, use {@link parseStream.toArray} function.
|
|
534
|
+
*
|
|
535
|
+
* @example Parsing CSV string stream
|
|
536
|
+
*
|
|
537
|
+
* ```ts
|
|
538
|
+
*
|
|
539
|
+
* import { parseStream } from 'web-csv-toolbox';
|
|
540
|
+
*
|
|
541
|
+
* const csv = `name,age
|
|
542
|
+
* Alice,42
|
|
543
|
+
* Bob,69`;
|
|
544
|
+
*
|
|
545
|
+
* const stream = new ReadableStream({
|
|
546
|
+
* start(controller) {
|
|
547
|
+
* controller.enqueue(csv);
|
|
548
|
+
* controller.close();
|
|
549
|
+
* },
|
|
550
|
+
* });
|
|
551
|
+
*
|
|
552
|
+
* for await (const record of parseStream(stream)) {
|
|
553
|
+
* console.log(record);
|
|
554
|
+
* }
|
|
555
|
+
* // Prints:
|
|
556
|
+
* // { name: 'Alice', age: '42' }
|
|
557
|
+
* // { name: 'Bob', age: '69' }
|
|
558
|
+
* ```
|
|
559
|
+
*
|
|
560
|
+
* @example Parsing CSV binary stream
|
|
561
|
+
*
|
|
562
|
+
* ```ts
|
|
563
|
+
* import { parseStream } from 'web-csv-toolbox';
|
|
564
|
+
*
|
|
565
|
+
* const csv = Uint8Array.from([
|
|
566
|
+
* // ...
|
|
567
|
+
* ]);
|
|
568
|
+
*
|
|
569
|
+
* const stream = new ReadableStream({
|
|
570
|
+
* start(controller) {
|
|
571
|
+
* controller.enqueue(csv);
|
|
572
|
+
* controller.close();
|
|
573
|
+
* },
|
|
574
|
+
* });
|
|
575
|
+
*
|
|
576
|
+
* for await (const record of parseStream(stream)) {
|
|
577
|
+
* console.log(record);
|
|
578
|
+
* }
|
|
579
|
+
* ```
|
|
580
|
+
*/
|
|
581
|
+
declare function parseStream<Header extends ReadonlyArray<string>>(
|
|
582
|
+
stream: ReadableStream<Uint8Array | string>,
|
|
583
|
+
options?: ParseBinaryOptions<Header>,
|
|
584
|
+
): AsyncIterableIterator<CSVRecord<Header>>;
|
|
585
|
+
declare namespace parseStream {
|
|
586
|
+
/**
|
|
587
|
+
* Parse CSV Stream to array of records.
|
|
588
|
+
*
|
|
589
|
+
* @returns Array of records
|
|
590
|
+
*/
|
|
591
|
+
function toArray<Header extends ReadonlyArray<string>>(
|
|
592
|
+
stream: ReadableStream<Uint8Array>,
|
|
593
|
+
options?: ParseBinaryOptions<Header>,
|
|
594
|
+
): Promise<CSVRecord<Header>[]>;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Parse CSV to records.
|
|
599
|
+
*
|
|
600
|
+
* {@link !String}, {@link !ReadableStream}<string | {@link !Uint8Array}> and {@link !Response} are supported.
|
|
601
|
+
*
|
|
602
|
+
*
|
|
603
|
+
* @typeParam Header Header type like `['name', 'age']`.
|
|
604
|
+
*
|
|
605
|
+
* @param csv CSV string to parse.
|
|
606
|
+
* @param options Parsing options for CSV string parsing.
|
|
607
|
+
* @returns Async iterable iterator of records.
|
|
608
|
+
*
|
|
609
|
+
* If you want array of records, use {@link parse.toArray} function.
|
|
610
|
+
* @category High-level API
|
|
611
|
+
*
|
|
612
|
+
* @remarks
|
|
613
|
+
* {@link parseString}, {@link parseBinaryStream},
|
|
614
|
+
* {@link parseStringStream} and {@link parseResponse} are used internally.
|
|
615
|
+
*
|
|
616
|
+
* If you known the type of the CSV, it performs better to use them directly.
|
|
617
|
+
*
|
|
618
|
+
* | If you want to parse a... | Use... | Data are treated as... |
|
|
619
|
+
* | ----------------------------------- | ------------------------- | ---------------------- |
|
|
620
|
+
* | {@link !String} | {@link parseString} | String |
|
|
621
|
+
* | {@link !ReadableStream}<string> | {@link parseStringStream} | String |
|
|
622
|
+
* | {@link !ReadableStream}<Uint8Array> | {@link parseBinaryStream} | Binary |
|
|
623
|
+
* | {@link !Response} | {@link parseResponse} | Binary |
|
|
624
|
+
*
|
|
625
|
+
* @example Parsing CSV files from strings
|
|
626
|
+
*
|
|
627
|
+
* ```ts
|
|
628
|
+
* import { parse } from 'web-csv-toolbox';
|
|
629
|
+
*
|
|
630
|
+
* const csv = `name,age
|
|
631
|
+
* Alice,42
|
|
632
|
+
* Bob,69`;
|
|
633
|
+
*
|
|
634
|
+
* for await (const record of parse(csv)) {
|
|
635
|
+
* console.log(record);
|
|
636
|
+
* }
|
|
637
|
+
* // Prints:
|
|
638
|
+
* // { name: 'Alice', age: '42' }
|
|
639
|
+
* // { name: 'Bob', age: '69' }
|
|
640
|
+
* ```
|
|
641
|
+
*
|
|
642
|
+
* @example Parsing CSV files from streams
|
|
643
|
+
*
|
|
644
|
+
* ```ts
|
|
645
|
+
* import { parse } from 'web-csv-toolbox';
|
|
646
|
+
*
|
|
647
|
+
* const csv = `name,age
|
|
648
|
+
* Alice,42
|
|
649
|
+
* Bob,69`;
|
|
650
|
+
*
|
|
651
|
+
* const stream = new ReadableStream({
|
|
652
|
+
* start(controller) {
|
|
653
|
+
* controller.enqueue(csv);
|
|
654
|
+
* controller.close();
|
|
655
|
+
* }
|
|
656
|
+
* });
|
|
657
|
+
*
|
|
658
|
+
* for await (const record of parse(stream)) {
|
|
659
|
+
* console.log(record);
|
|
660
|
+
* }
|
|
661
|
+
* // Prints:
|
|
662
|
+
* // { name: 'Alice', age: '42' }
|
|
663
|
+
* // { name: 'Bob', age: '69' }
|
|
664
|
+
* ```
|
|
665
|
+
*
|
|
666
|
+
*
|
|
667
|
+
* @example Parsing CSV files with headers
|
|
668
|
+
*
|
|
669
|
+
* ```ts
|
|
670
|
+
* import { parse } from 'web-csv-toolbox';
|
|
671
|
+
*
|
|
672
|
+
* // This CSV has no header.
|
|
673
|
+
* const csv = `Alice,42
|
|
674
|
+
* Bob,69`;
|
|
675
|
+
*
|
|
676
|
+
* for await (const record of parse(csv, { header: ['name', 'age'] })) {
|
|
677
|
+
* console.log(record);
|
|
678
|
+
* }
|
|
679
|
+
* // Prints:
|
|
680
|
+
* // { name: 'Alice', age: '42' }
|
|
681
|
+
* // { name: 'Bob', age: '69' }
|
|
682
|
+
* ```
|
|
683
|
+
*
|
|
684
|
+
* @example Parsing CSV files with different delimiters characters
|
|
685
|
+
*
|
|
686
|
+
* ```ts
|
|
687
|
+
* import { parse } from 'web-csv-toolbox';
|
|
688
|
+
*
|
|
689
|
+
* const csv = `name\tage
|
|
690
|
+
* Alice\t42
|
|
691
|
+
* Bob\t69`;
|
|
692
|
+
*
|
|
693
|
+
* for await (const record of parse(csv, { delimiter: '\t' })) {
|
|
694
|
+
* console.log(record);
|
|
695
|
+
* }
|
|
696
|
+
* // Prints:
|
|
697
|
+
* // { name: 'Alice', age: '42' }
|
|
698
|
+
* // { name: 'Bob', age: '69' }
|
|
699
|
+
* ```
|
|
307
700
|
*/
|
|
308
701
|
declare function parse<Header extends ReadonlyArray<string>>(
|
|
309
|
-
csv: string | ReadableStream<
|
|
702
|
+
csv: string | ReadableStream<string>,
|
|
310
703
|
options?: ParseOptions<Header>,
|
|
311
704
|
): AsyncIterableIterator<CSVRecord<Header>>;
|
|
705
|
+
/**
|
|
706
|
+
* Parse CSV binary to records.
|
|
707
|
+
*
|
|
708
|
+
* @param csv CSV binary to parse.
|
|
709
|
+
* @param options Parsing options for CSV binary parsing.
|
|
710
|
+
*
|
|
711
|
+
* @example Parsing CSV files from responses
|
|
712
|
+
*
|
|
713
|
+
* ```ts
|
|
714
|
+
* import { parse } from 'web-csv-toolbox';
|
|
715
|
+
*
|
|
716
|
+
* // This CSV data is not gzipped and encoded in utf-8.
|
|
717
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
718
|
+
*
|
|
719
|
+
* for await (const record of parse(response)) {
|
|
720
|
+
* // ...
|
|
721
|
+
* }
|
|
722
|
+
* ```
|
|
723
|
+
*
|
|
724
|
+
* @example Parsing CSV files with options spcialized for binary
|
|
725
|
+
*
|
|
726
|
+
* ```ts
|
|
727
|
+
* import { parse } from 'web-csv-toolbox';
|
|
728
|
+
*
|
|
729
|
+
* // This CSV data is gzipped and encoded in shift-jis and has BOM.
|
|
730
|
+
* const response = await fetch('https://example.com/data.csv.gz');
|
|
731
|
+
*
|
|
732
|
+
* for await (const record of parse(response, {
|
|
733
|
+
* charset: 'shift-jis',
|
|
734
|
+
* ignoreBOM: true,
|
|
735
|
+
* decomposition: 'gzip',
|
|
736
|
+
* })) {
|
|
737
|
+
* // ...
|
|
738
|
+
* }
|
|
739
|
+
* ```
|
|
740
|
+
*/
|
|
741
|
+
declare function parse<Header extends ReadonlyArray<string>>(
|
|
742
|
+
csv: ReadableStream<Uint8Array> | Response,
|
|
743
|
+
options?: ParseBinaryOptions<Header>,
|
|
744
|
+
): AsyncIterableIterator<CSVRecord<Header>>;
|
|
312
745
|
declare namespace parse {
|
|
746
|
+
/**
|
|
747
|
+
* Parse CSV string to array of records,
|
|
748
|
+
* ideal for smaller data sets.
|
|
749
|
+
*
|
|
750
|
+
* @example Parse a CSV as array of records
|
|
751
|
+
*
|
|
752
|
+
* ```ts
|
|
753
|
+
* import { parse } from 'web-csv-toolbox';
|
|
754
|
+
*
|
|
755
|
+
* const csv = `name,age
|
|
756
|
+
* Alice,42
|
|
757
|
+
* Bob,69`;
|
|
758
|
+
*
|
|
759
|
+
* const records = await parse.toArray(csv);
|
|
760
|
+
* console.log(records);
|
|
761
|
+
* // Prints:
|
|
762
|
+
* // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
|
|
763
|
+
* ```
|
|
764
|
+
*/
|
|
765
|
+
function toArray<Header extends ReadonlyArray<string>>(
|
|
766
|
+
csv: string | ReadableStream<string>,
|
|
767
|
+
options?: ParseOptions<Header>,
|
|
768
|
+
): Promise<CSVRecord<Header>[]>;
|
|
769
|
+
/**
|
|
770
|
+
* Parse CSV string to array of records,
|
|
771
|
+
* ideal for smaller data sets.
|
|
772
|
+
*
|
|
773
|
+
* @example Parse a CSV as array of records
|
|
774
|
+
*
|
|
775
|
+
* ```ts
|
|
776
|
+
* import { parse } from 'web-csv-toolbox';
|
|
777
|
+
*
|
|
778
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
779
|
+
*
|
|
780
|
+
* const records = await parse.toArray(response);
|
|
781
|
+
* console.log(records);
|
|
782
|
+
* ```
|
|
783
|
+
*/
|
|
313
784
|
function toArray<Header extends ReadonlyArray<string>>(
|
|
314
|
-
csv:
|
|
785
|
+
csv: ReadableStream<Uint8Array> | Response,
|
|
315
786
|
options?: ParseOptions<Header>,
|
|
316
787
|
): Promise<CSVRecord<Header>[]>;
|
|
317
788
|
}
|
|
@@ -332,6 +803,8 @@ export {
|
|
|
332
803
|
type TokenType,
|
|
333
804
|
parse,
|
|
334
805
|
parseBinaryStream,
|
|
806
|
+
parseResponse,
|
|
807
|
+
parseStream,
|
|
808
|
+
parseString,
|
|
335
809
|
parseStringStream,
|
|
336
|
-
streamingParse,
|
|
337
810
|
};
|