web-csv-toolbox 0.5.3 → 0.6.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.
Files changed (167) hide show
  1. package/dist/cjs/Lexer.cjs +2 -0
  2. package/dist/cjs/Lexer.cjs.map +1 -0
  3. package/dist/cjs/LexerTransformer.cjs +2 -0
  4. package/dist/cjs/LexerTransformer.cjs.map +1 -0
  5. package/dist/cjs/RecordAssembler.cjs +2 -0
  6. package/dist/cjs/RecordAssembler.cjs.map +1 -0
  7. package/dist/cjs/RecordAssemblerTransformer.cjs +2 -0
  8. package/dist/cjs/RecordAssemblerTransformer.cjs.map +1 -0
  9. package/dist/cjs/assertCommonOptions.cjs +2 -0
  10. package/dist/cjs/assertCommonOptions.cjs.map +1 -0
  11. package/dist/cjs/common/constants.cjs +2 -0
  12. package/dist/cjs/common/constants.cjs.map +1 -0
  13. package/dist/cjs/constants.cjs +2 -0
  14. package/dist/cjs/constants.cjs.map +1 -0
  15. package/dist/cjs/convertBinaryToString.cjs +2 -0
  16. package/dist/cjs/convertBinaryToString.cjs.map +1 -0
  17. package/dist/cjs/getOptionsFromResponse.cjs +2 -0
  18. package/dist/cjs/getOptionsFromResponse.cjs.map +1 -0
  19. package/dist/cjs/parse.cjs +2 -0
  20. package/dist/cjs/parse.cjs.map +1 -0
  21. package/dist/cjs/parseBinary.cjs +2 -0
  22. package/dist/cjs/parseBinary.cjs.map +1 -0
  23. package/dist/cjs/parseBinaryToArraySync.cjs +2 -0
  24. package/dist/cjs/parseBinaryToArraySync.cjs.map +1 -0
  25. package/dist/cjs/parseBinaryToIterableIterator.cjs +2 -0
  26. package/dist/cjs/parseBinaryToIterableIterator.cjs.map +1 -0
  27. package/dist/cjs/parseBinaryToStream.cjs +2 -0
  28. package/dist/cjs/parseBinaryToStream.cjs.map +1 -0
  29. package/dist/cjs/parseResponse.cjs +2 -0
  30. package/dist/cjs/parseResponse.cjs.map +1 -0
  31. package/dist/cjs/parseResponseToStream.cjs +2 -0
  32. package/dist/cjs/parseResponseToStream.cjs.map +1 -0
  33. package/dist/cjs/parseString.cjs +2 -0
  34. package/dist/cjs/parseString.cjs.map +1 -0
  35. package/dist/cjs/parseStringStream.cjs +2 -0
  36. package/dist/cjs/parseStringStream.cjs.map +1 -0
  37. package/dist/cjs/parseStringStreamToStream.cjs +2 -0
  38. package/dist/cjs/parseStringStreamToStream.cjs.map +1 -0
  39. package/dist/cjs/parseStringToArraySync.cjs +2 -0
  40. package/dist/cjs/parseStringToArraySync.cjs.map +1 -0
  41. package/dist/cjs/parseStringToIterableIterator.cjs +2 -0
  42. package/dist/cjs/parseStringToIterableIterator.cjs.map +1 -0
  43. package/dist/cjs/parseStringToStream.cjs +2 -0
  44. package/dist/cjs/parseStringToStream.cjs.map +1 -0
  45. package/dist/cjs/parseUint8ArrayStream.cjs +2 -0
  46. package/dist/cjs/parseUint8ArrayStream.cjs.map +1 -0
  47. package/dist/cjs/parseUint8ArrayStreamToStream.cjs +2 -0
  48. package/dist/cjs/parseUint8ArrayStreamToStream.cjs.map +1 -0
  49. package/dist/cjs/utils/convertIterableIteratorToAsync.cjs +2 -0
  50. package/dist/cjs/utils/convertIterableIteratorToAsync.cjs.map +1 -0
  51. package/dist/cjs/utils/convertStreamToAsyncIterableIterator.cjs +2 -0
  52. package/dist/cjs/utils/convertStreamToAsyncIterableIterator.cjs.map +1 -0
  53. package/dist/cjs/utils/convertThisAsyncIterableIteratorToArray.cjs +2 -0
  54. package/dist/cjs/utils/convertThisAsyncIterableIteratorToArray.cjs.map +1 -0
  55. package/dist/cjs/utils/escapeRegExp.cjs +2 -0
  56. package/dist/cjs/utils/escapeRegExp.cjs.map +1 -0
  57. package/dist/cjs/utils/parseMime.cjs +2 -0
  58. package/dist/cjs/utils/parseMime.cjs.map +1 -0
  59. package/dist/cjs/utils/pipeline.cjs +2 -0
  60. package/dist/cjs/utils/pipeline.cjs.map +1 -0
  61. package/dist/cjs/web-csv-toolbox.cjs +2 -0
  62. package/dist/cjs/web-csv-toolbox.cjs.map +1 -0
  63. package/dist/es/Lexer.js +151 -0
  64. package/dist/es/Lexer.js.map +1 -0
  65. package/dist/es/LexerTransformer.js +20 -0
  66. package/dist/es/LexerTransformer.js.map +1 -0
  67. package/dist/es/RecordAssembler.js +72 -0
  68. package/dist/es/RecordAssembler.js.map +1 -0
  69. package/dist/es/RecordAssemblerTransformer.js +22 -0
  70. package/dist/es/RecordAssemblerTransformer.js.map +1 -0
  71. package/dist/es/assertCommonOptions.js +24 -0
  72. package/dist/es/assertCommonOptions.js.map +1 -0
  73. package/dist/es/common/constants.js +6 -0
  74. package/dist/es/common/constants.js.map +1 -0
  75. package/dist/es/constants.js +8 -0
  76. package/dist/es/constants.js.map +1 -0
  77. package/dist/es/convertBinaryToString.js +9 -0
  78. package/dist/es/convertBinaryToString.js.map +1 -0
  79. package/dist/es/getOptionsFromResponse.js +20 -0
  80. package/dist/es/getOptionsFromResponse.js.map +1 -0
  81. package/dist/es/parse.js +39 -0
  82. package/dist/es/parse.js.map +1 -0
  83. package/dist/es/parseBinary.js +35 -0
  84. package/dist/es/parseBinary.js.map +1 -0
  85. package/dist/es/parseBinaryToArraySync.js +10 -0
  86. package/dist/es/parseBinaryToArraySync.js.map +1 -0
  87. package/dist/es/parseBinaryToIterableIterator.js +10 -0
  88. package/dist/es/parseBinaryToIterableIterator.js.map +1 -0
  89. package/dist/es/parseBinaryToStream.js +10 -0
  90. package/dist/es/parseBinaryToStream.js.map +1 -0
  91. package/dist/es/parseResponse.js +27 -0
  92. package/dist/es/parseResponse.js.map +1 -0
  93. package/dist/es/parseResponseToStream.js +13 -0
  94. package/dist/es/parseResponseToStream.js.map +1 -0
  95. package/dist/es/parseString.js +33 -0
  96. package/dist/es/parseString.js.map +1 -0
  97. package/dist/es/parseStringStream.js +23 -0
  98. package/dist/es/parseStringStream.js.map +1 -0
  99. package/dist/es/parseStringStreamToStream.js +14 -0
  100. package/dist/es/parseStringStreamToStream.js.map +1 -0
  101. package/dist/es/parseStringToArraySync.js +12 -0
  102. package/dist/es/parseStringToArraySync.js.map +1 -0
  103. package/dist/es/parseStringToIterableIterator.js +12 -0
  104. package/dist/es/parseStringToIterableIterator.js.map +1 -0
  105. package/dist/es/parseStringToStream.js +19 -0
  106. package/dist/es/parseStringToStream.js.map +1 -0
  107. package/dist/es/parseUint8ArrayStream.js +23 -0
  108. package/dist/es/parseUint8ArrayStream.js.map +1 -0
  109. package/dist/es/parseUint8ArrayStreamToStream.js +22 -0
  110. package/dist/es/parseUint8ArrayStreamToStream.js.map +1 -0
  111. package/dist/es/utils/convertIterableIteratorToAsync.js +14 -0
  112. package/dist/es/utils/convertIterableIteratorToAsync.js.map +1 -0
  113. package/dist/es/utils/convertStreamToAsyncIterableIterator.js +12 -0
  114. package/dist/es/utils/convertStreamToAsyncIterableIterator.js.map +1 -0
  115. package/dist/es/utils/convertThisAsyncIterableIteratorToArray.js +10 -0
  116. package/dist/es/utils/convertThisAsyncIterableIteratorToArray.js.map +1 -0
  117. package/dist/es/utils/escapeRegExp.js +6 -0
  118. package/dist/es/utils/escapeRegExp.js.map +1 -0
  119. package/dist/es/utils/parseMime.js +15 -0
  120. package/dist/es/utils/parseMime.js.map +1 -0
  121. package/dist/es/utils/pipeline.js +18 -0
  122. package/dist/es/utils/pipeline.js.map +1 -0
  123. package/dist/es/web-csv-toolbox.js +10 -0
  124. package/dist/es/web-csv-toolbox.js.map +1 -0
  125. package/dist/types/Lexer.d.ts +7 -0
  126. package/dist/types/LexerTransformer.d.ts +34 -0
  127. package/dist/types/RecordAssembler.d.ts +7 -0
  128. package/dist/types/RecordAssemblerTransformer.d.ts +47 -0
  129. package/dist/types/assertCommonOptions.d.ts +7 -0
  130. package/dist/types/common/constants.d.ts +15 -0
  131. package/dist/types/common/types.d.ts +163 -0
  132. package/dist/types/constants.d.ts +11 -0
  133. package/dist/types/convertBinaryToString.d.ts +2 -0
  134. package/dist/types/escapeField.d.ts +13 -0
  135. package/dist/types/getOptionsFromResponse.d.ts +2 -0
  136. package/dist/types/parse.d.ts +182 -0
  137. package/dist/types/parseBinary.d.ts +113 -0
  138. package/dist/types/parseBinaryToArraySync.d.ts +2 -0
  139. package/dist/types/parseBinaryToIterableIterator.d.ts +2 -0
  140. package/dist/types/parseBinaryToStream.d.ts +2 -0
  141. package/dist/types/parseResponse.d.ts +80 -0
  142. package/dist/types/parseResponseToStream.d.ts +2 -0
  143. package/dist/types/parseString.d.ts +120 -0
  144. package/dist/types/parseStringStream.d.ts +98 -0
  145. package/dist/types/parseStringStreamToStream.d.ts +2 -0
  146. package/dist/types/parseStringToArraySync.d.ts +2 -0
  147. package/dist/types/parseStringToIterableIterator.d.ts +2 -0
  148. package/dist/types/parseStringToStream.d.ts +2 -0
  149. package/dist/types/parseUint8ArrayStream.d.ts +94 -0
  150. package/dist/types/parseUint8ArrayStreamToStream.d.ts +2 -0
  151. package/dist/types/utils/SingleValueReadableStream.d.ts +3 -0
  152. package/dist/types/utils/convertIterableIteratorToAsync.d.ts +1 -0
  153. package/dist/types/utils/convertStreamToAsyncIterableIterator.d.ts +1 -0
  154. package/dist/types/utils/convertThisAsyncIterableIteratorToArray.d.ts +1 -0
  155. package/dist/types/utils/escapeRegExp.d.ts +8 -0
  156. package/dist/types/utils/occurrences.d.ts +7 -0
  157. package/dist/types/utils/parseMime.d.ts +7 -0
  158. package/dist/types/utils/pipeline.d.ts +3 -0
  159. package/dist/types/web-csv-toolbox.d.ts +10 -0
  160. package/dist/web-csv-toolbox.umd.cjs +1 -1
  161. package/dist/web-csv-toolbox.umd.cjs.map +1 -1
  162. package/package.json +31 -10
  163. package/dist/web-csv-toolbox.cjs +0 -2
  164. package/dist/web-csv-toolbox.cjs.map +0 -1
  165. package/dist/web-csv-toolbox.d.ts +0 -982
  166. package/dist/web-csv-toolbox.js +0 -576
  167. package/dist/web-csv-toolbox.js.map +0 -1
@@ -1,982 +0,0 @@
1
- /**
2
- * CSV Parsing Options for binary.
3
- * @category Types
4
- */
5
- export declare interface BinaryOptions {
6
- /**
7
- * If the binary is compressed by a compression algorithm,
8
- * the decompressed CSV can be parsed by specifying the algorithm.
9
- *
10
- * @remarks
11
- * Make sure the runtime you are running supports stream decompression.
12
- *
13
- * See {@link https://developer.mozilla.org/en-US/docs/Web/API/DecompressionStream#browser_compatibility | DecompressionStream Compatibility}.
14
- */
15
- decomposition?: CompressionFormat;
16
- /**
17
- * You can specify the character encoding of the binary.
18
- *
19
- * @remarks
20
- * {@link !TextDecoderStream} is used internally.
21
- *
22
- * See {@link https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings | Encoding API Compatibility}
23
- * for the encoding formats that can be specified.
24
- *
25
- * @default 'utf-8'
26
- */
27
- charset?: string;
28
- /**
29
- * If the binary has a BOM, you can specify whether to ignore it.
30
- *
31
- * @remarks
32
- * If you specify true, the BOM will be ignored.
33
- * If you specify false or not specify it, the BOM will be treated as a normal character.
34
- * See {@link https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream/ignoreBOM | TextDecoderOptions.ignoreBOM} for more information about the BOM.
35
- * @default false
36
- */
37
- ignoreBOM?: boolean;
38
- /**
39
- * If the binary has a invalid character, you can specify whether to throw an error.
40
- *
41
- * @remarks
42
- * If the property is `true` then a decoder will throw a {@link !TypeError}
43
- * if it encounters malformed data while decoding.
44
- *
45
- * If `false` the decoder will substitute the invalid data
46
- * with the replacement character `U+FFFD` (�).
47
- *
48
- * See {@link https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream/fatal | TextDecoderOptions.fatal} for more information.
49
- *
50
- * @default false
51
- */
52
- fatal?: boolean;
53
- }
54
-
55
- /**
56
- * CSV Common Options.
57
- * @category Types
58
- */
59
- export declare interface CommonOptions {
60
- /**
61
- * CSV field delimiter.
62
- *
63
- * @remarks
64
- * If you want to parse TSV, specify `'\t'`.
65
- *
66
- * This library supports multi-character delimiters.
67
- * @default ','
68
- */
69
- delimiter?: string;
70
- /**
71
- * CSV field quotation.
72
- *
73
- * @remarks
74
- * This library supports multi-character quotations.
75
- *
76
- * @default '"'
77
- */
78
- quotation?: string;
79
- }
80
-
81
- /**
82
- * CSV.
83
- *
84
- * @category Types
85
- */
86
- export declare type CSV = CSVString | CSVBinary;
87
-
88
- /**
89
- * CSV Binary.
90
- *
91
- * @category Types
92
- */
93
- export declare type CSVBinary = ReadableStream<Uint8Array> | Response | ArrayBuffer | Uint8Array;
94
-
95
- /**
96
- * CSV Record.
97
- * @category Types
98
- * @template Header Header of the CSV.
99
- *
100
- * @example Header is ["foo", "bar"]
101
- * ```ts
102
- * const record: CSVRecord<["foo", "bar"]> = {
103
- * foo: "1",
104
- * bar: "2",
105
- * };
106
- * ```
107
- */
108
- export declare type CSVRecord<Header extends ReadonlyArray<string>> = Record<Header[number], string>;
109
-
110
- /**
111
- * CSV String.
112
- *
113
- * @category Types
114
- */
115
- export declare type CSVString = string | ReadableStream<string>;
116
-
117
- /**
118
- * Field is a symbol for field of CSV.
119
- * @category Constants
120
- */
121
- export declare const Field: unique symbol;
122
-
123
- /**
124
- * FiledDelimiter is a symbol for field delimiter of CSV.
125
- * @category Constants
126
- */
127
- export declare const FieldDelimiter: unique symbol;
128
-
129
- /**
130
- * Field delimiter token type.
131
- * @category Types
132
- */
133
- export declare interface FieldDelimiterToken {
134
- type: typeof FieldDelimiter;
135
- }
136
-
137
- /**
138
- * Field token type.
139
- * @category Types
140
- */
141
- export declare interface FieldToken {
142
- type: typeof Field;
143
- value: string;
144
- }
145
-
146
- /**
147
- * A transform stream that converts a stream of tokens into a stream of rows.
148
- *
149
- * @category Low-level API
150
- *
151
- * @example Parse a CSV with headers by data
152
- * ```ts
153
- * new ReadableStream({
154
- * start(controller) {
155
- * controller.enqueue("name,age\r\n");
156
- * controller.enqueue("Alice,20\r\n");
157
- * controller.close();
158
- * }
159
- * })
160
- * .pipeThrough(new LexerTransformer())
161
- * .pipeTo(new WritableStream({ write(tokens) {
162
- * for (const token of tokens) {
163
- * console.log(token);
164
- * }
165
- * }}));
166
- * // { type: Field, value: "name" }
167
- * // FieldDelimiter
168
- * // { type: Field, value: "age" }
169
- * // RecordDelimiter
170
- * // { type: Field, value: "Alice" }
171
- * // FieldDelimiter
172
- * // { type: Field, value: "20" }
173
- * // RecordDelimiter
174
- * ```
175
- */
176
- export declare class LexerTransformer extends TransformStream<string, Token[]> {
177
- constructor(options?: CommonOptions);
178
- }
179
-
180
- /**
181
- * Parse CSV to records.
182
- *
183
- * {@link !String}, {@link !ReadableStream}<string | {@link !Uint8Array}> and {@link !Response} are supported.
184
- *
185
- *
186
- * @typeParam Header Header type like `['name', 'age']`.
187
- *
188
- * @param csv CSV string to parse.
189
- * @param options Parsing options for CSV string parsing.
190
- * @returns Async iterable iterator of records.
191
- *
192
- * If you want array of records, use {@link parse.toArray} function.
193
- * @category High-level API
194
- *
195
- * @remarks
196
- * {@link parseString}, {@link parseBinary}, {@link parseUint8ArrayStream},
197
- * {@link parseStringStream} and {@link parseResponse} are used internally.
198
- *
199
- * If you known the type of the CSV, it performs better to use them directly.
200
- *
201
- * | If you want to parse a... | Use... | Options... |
202
- * | -------------------------------------------- | ----------------------------- | -------------------------- |
203
- * | {@link !String} | {@link parseString} | {@link ParseOptions} |
204
- * | {@link !ReadableStream}<{@link !String}> | {@link parseStringStream} | {@link ParseOptions} |
205
- * | {@link !Uint8Array} \| {@link !ArrayBuffer} | {@link parseBinary} | {@link ParseBinaryOptions} |
206
- * | {@link !ReadableStream}<{@link !Uint8Array}> | {@link parseUint8ArrayStream} | {@link ParseBinaryOptions} |
207
- * | {@link !Response} | {@link parseResponse} | {@link ParseBinaryOptions} |
208
- *
209
- * @example Parsing CSV files from strings
210
- *
211
- * ```ts
212
- * import { parse } from 'web-csv-toolbox';
213
- *
214
- * const csv = `name,age
215
- * Alice,42
216
- * Bob,69`;
217
- *
218
- * for await (const record of parse(csv)) {
219
- * console.log(record);
220
- * }
221
- * // Prints:
222
- * // { name: 'Alice', age: '42' }
223
- * // { name: 'Bob', age: '69' }
224
- * ```
225
- *
226
- * @example Parsing CSV files from streams
227
- *
228
- * ```ts
229
- * import { parse } from 'web-csv-toolbox';
230
- *
231
- * const csv = `name,age
232
- * Alice,42
233
- * Bob,69`;
234
- *
235
- * const stream = new ReadableStream({
236
- * start(controller) {
237
- * controller.enqueue(csv);
238
- * controller.close();
239
- * }
240
- * });
241
- *
242
- * for await (const record of parse(stream)) {
243
- * console.log(record);
244
- * }
245
- * // Prints:
246
- * // { name: 'Alice', age: '42' }
247
- * // { name: 'Bob', age: '69' }
248
- * ```
249
- *
250
- *
251
- * @example Parsing CSV files with headers
252
- *
253
- * ```ts
254
- * import { parse } from 'web-csv-toolbox';
255
- *
256
- * // This CSV has no header.
257
- * const csv = `Alice,42
258
- * Bob,69`;
259
- *
260
- * for await (const record of parse(csv, { header: ['name', 'age'] })) {
261
- * console.log(record);
262
- * }
263
- * // Prints:
264
- * // { name: 'Alice', age: '42' }
265
- * // { name: 'Bob', age: '69' }
266
- * ```
267
- *
268
- * @example Parsing CSV files with different delimiters characters
269
- *
270
- * ```ts
271
- * import { parse } from 'web-csv-toolbox';
272
- *
273
- * const csv = `name\tage
274
- * Alice\t42
275
- * Bob\t69`;
276
- *
277
- * for await (const record of parse(csv, { delimiter: '\t' })) {
278
- * console.log(record);
279
- * }
280
- * // Prints:
281
- * // { name: 'Alice', age: '42' }
282
- * // { name: 'Bob', age: '69' }
283
- * ```
284
- */
285
- export declare function parse<Header extends ReadonlyArray<string>>(csv: CSVString, options?: ParseOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
286
-
287
- /**
288
- * Parse CSV binary to records.
289
- *
290
- * @param csv CSV binary to parse.
291
- * @param options Parsing options for CSV binary parsing.
292
- *
293
- * @example Parsing CSV files from responses
294
- *
295
- * ```ts
296
- * import { parse } from 'web-csv-toolbox';
297
- *
298
- * // This CSV data is not gzipped and encoded in utf-8.
299
- * const response = await fetch('https://example.com/data.csv');
300
- *
301
- * for await (const record of parse(response)) {
302
- * // ...
303
- * }
304
- * ```
305
- *
306
- * @example Parsing CSV files with options spcialized for binary
307
- *
308
- * ```ts
309
- * import { parse } from 'web-csv-toolbox';
310
- *
311
- * // This CSV data is gzipped and encoded in shift-jis and has BOM.
312
- * const response = await fetch('https://example.com/data.csv.gz');
313
- *
314
- * for await (const record of parse(response, {
315
- * charset: 'shift-jis',
316
- * ignoreBOM: true,
317
- * decomposition: 'gzip',
318
- * })) {
319
- * // ...
320
- * }
321
- * ```
322
- */
323
- export declare function parse<Header extends ReadonlyArray<string>>(csv: CSVBinary, options?: ParseBinaryOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
324
-
325
- export declare namespace parse {
326
- /**
327
- * Parse CSV string to array of records,
328
- * ideal for smaller data sets.
329
- *
330
- * @example Parse a CSV as array of records
331
- *
332
- * ```ts
333
- * import { parse } from 'web-csv-toolbox';
334
- *
335
- * const csv = `name,age
336
- * Alice,42
337
- * Bob,69`;
338
- *
339
- * const records = await parse.toArray(csv);
340
- * console.log(records);
341
- * // Prints:
342
- * // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
343
- * ```
344
- */
345
- export function toArray<Header extends ReadonlyArray<string>>(csv: CSVString, options?: ParseOptions<Header>): Promise<CSVRecord<Header>[]>;
346
- /**
347
- * Parse CSV string to array of records,
348
- * ideal for smaller data sets.
349
- *
350
- * @example Parse a CSV as array of records
351
- *
352
- * ```ts
353
- * import { parse } from 'web-csv-toolbox';
354
- *
355
- * const response = await fetch('https://example.com/data.csv');
356
- *
357
- * const records = await parse.toArray(response);
358
- * console.log(records);
359
- * ```
360
- */
361
- export function toArray<Header extends ReadonlyArray<string>>(csv: CSVBinary, options?: ParseBinaryOptions<Header>): Promise<CSVRecord<Header>[]>;
362
- }
363
-
364
- /**
365
- * Parse a binary from an {@link !Uint8Array}.
366
- *
367
- * @category Middle-level API
368
- *
369
- * @param bytes CSV bytes to parse.
370
- * @param options Parsing options
371
- * @returns Async iterable iterator of records.
372
- *
373
- * @example Parsing CSV binary
374
- *
375
- * ```ts
376
- * import { parseUint8Array } from 'web-csv-toolbox';
377
- *
378
- * const csv = Uint8Array.from([
379
- * // ...
380
- * ]);
381
- *
382
- * for await (const record of parseUint8Array(csv)) {
383
- * console.log(record);
384
- * }
385
- * ```
386
- */
387
- export declare function parseBinary<Header extends ReadonlyArray<string>>(bytes: Uint8Array | ArrayBuffer, options?: ParseBinaryOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
388
-
389
- export declare namespace parseBinary {
390
- /**
391
- * Parse a binary from an {@link !Uint8Array} to an array of records.
392
- *
393
- * @param bytes CSV bytes to parse.
394
- * @param options Parsing options
395
- * @returns Array of records
396
- *
397
- * @example
398
- * ```ts
399
- * import { parseUint8Array } from 'web-csv-toolbox';
400
- *
401
- * const csv = Uint8Array.from([
402
- * // ...
403
- * ]);
404
- *
405
- * const records = await parseUint8Array.toArray(csv);
406
- * ```
407
- */
408
- export function toArray<Header extends ReadonlyArray<string>>(bytes: Uint8Array | ArrayBuffer, options?: ParseBinaryOptions<Header>): Promise<CSVRecord<Header>[]>;
409
- /**
410
- * Parse a binary from an {@link !Uint8Array} to an array of records.
411
- *
412
- * @param bytes CSV bytes to parse.
413
- * @param options Parsing options
414
- * @returns Array of records
415
- * @example
416
- *
417
- * ```ts
418
- * import { parseUint8Array } from 'web-csv-toolbox';
419
- *
420
- * const csv = Uint8Array.from([
421
- * // ...
422
- * ]);
423
- *
424
- * const records = parseUint8Array.toArraySync(csv);
425
- * ```
426
- */
427
- export function toArraySync<Header extends ReadonlyArray<string>>(bytes: Uint8Array | ArrayBuffer, options?: ParseBinaryOptions<Header>): CSVRecord<Header>[];
428
- /**
429
- * Parse a binary from an {@link !Uint8Array} to an iterable iterator of records.
430
- *
431
- * @param bytes CSV bytes to parse.
432
- * @param options Parsing options
433
- * @returns Async iterable iterator of records.
434
- * @example
435
- * ```ts
436
- * import { parseUint8Array } from 'web-csv-toolbox';
437
- *
438
- * const csv = Uint8Array.from([
439
- * // ...
440
- * ]);
441
- *
442
- * for (const record of parseUint8Array.toIterableIterator(csv)) {
443
- * console.log(record);
444
- * }
445
- * ```
446
- */
447
- export function toIterableIterator<Header extends ReadonlyArray<string>>(bytes: Uint8Array, options?: ParseBinaryOptions<Header>): IterableIterator<CSVRecord<Header>>;
448
- /**
449
- * Parse a binary from an {@link !Uint8Array} to a stream of records.
450
- *
451
- * @param bytes CSV bytes to parse.
452
- * @param options Parsing options
453
- * @returns Stream of records.
454
- *
455
- * @example
456
- *
457
- * ```ts
458
- * import { parseUint8Array } from 'web-csv-toolbox';
459
- *
460
- * const csv = Uint8Array.from([
461
- * // ...
462
- * ]);
463
- *
464
- * const stream = parseUint8Array.toStream(csv);
465
- *
466
- * await stream.pipeTo(
467
- * new WritableStream({
468
- * write(record) {
469
- * console.log(record);
470
- * },
471
- * }),
472
- * );
473
- * ```
474
- */
475
- export function toStream<Header extends ReadonlyArray<string>>(bytes: Uint8Array, options?: ParseBinaryOptions<Header>): ReadableStream<CSVRecord<Header>>;
476
- }
477
-
478
- /**
479
- * Parse options for CSV binary.
480
- * @category Types
481
- */
482
- export declare interface ParseBinaryOptions<Header extends ReadonlyArray<string>> extends ParseOptions<Header>, BinaryOptions {
483
- }
484
-
485
- /**
486
- * Parse options for CSV string.
487
- * @category Types
488
- */
489
- export declare interface ParseOptions<Header extends ReadonlyArray<string>> extends CommonOptions, RecordAssemblerOptions<Header> {
490
- }
491
-
492
- /**
493
- * Parse HTTP Response what contains CSV to records,
494
- * ideal for smaller data sets.
495
- *
496
- * @remarks
497
- * This function automatically treats response headers.
498
- *
499
- * - If `Content-Type` header is not set, it assumes `text/csv`.
500
- * - If `Content-Type` header is not `text/csv`, it throws an error.
501
- * - If `Content-Type` header has charset parameter, it uses it for decoding.
502
- * - If `Content-Encoding` header is set, it decompresses the response.
503
- * - Should there be any conflicting information between the header and the options, the option's value will take precedence.
504
- *
505
- * @category Middle-level API
506
- * @param response
507
- * @param options
508
- * @returns Async iterable iterator of records.
509
- *
510
- * If you want array of records, use {@link parseResponse.toArray} function.
511
- *
512
- * @example Parsing CSV Response
513
- *
514
- * ```ts
515
- * import { parseResponse } from 'web-csv-toolbox';
516
- *
517
- * const response = await fetch('https://example.com/data.csv');
518
- *
519
- * for await (const record of parseResponse(response)) {
520
- * console.log(record);
521
- * }
522
- * ```
523
- */
524
- export declare function parseResponse<Header extends ReadonlyArray<string>>(response: Response, options?: ParseOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
525
-
526
- export declare namespace parseResponse {
527
- /**
528
- * Parse CSV Response to array of records.
529
- *
530
- * @returns Array of records
531
- *
532
- * @example Parsing CSV Response
533
- *
534
- * ```ts
535
- * import { parseResponse } from 'web-csv-toolbox';
536
- *
537
- * const response = await fetch('https://example.com/data.csv');
538
- *
539
- * const records = await parseResponse.toArray(response);
540
- * console.log(records);
541
- * ```
542
- */
543
- export function toArray<Header extends ReadonlyArray<string>>(response: Response, options?: ParseOptions<Header>): Promise<CSVRecord<Header>[]>;
544
- /**
545
- * Parse CSV Response to stream of records.
546
- *
547
- * @param response Response to parse
548
- * @returns Stream of records
549
- *
550
- * @example Parsing CSV Response
551
- *
552
- * ```ts
553
- * import { parseResponse } from 'web-csv-toolbox';
554
- *
555
- * const response = await fetch('https://example.com/data.csv');
556
- *
557
- * await parseResponse.toStream(response)
558
- * .pipeTo(
559
- * new WritableStream({
560
- * write(record) {
561
- * console.log(record);
562
- * },
563
- * }),
564
- * );
565
- * // Prints:
566
- * // { name: 'Alice', age: '42' }
567
- * // { name: 'Bob', age: '69' }
568
- * ```
569
- */
570
- export function toStream<Header extends ReadonlyArray<string>>(response: Response, options?: ParseOptions<Header>): ReadableStream<CSVRecord<Header>[]>;
571
- }
572
-
573
- /**
574
- * Parse CSV string to records.
575
- *
576
- * @category Middle-level API
577
- * @param csv CSV string to parse
578
- * @param options Parsing options. See {@link ParseOptions}.
579
- * @returns Async iterable iterator of records.
580
- *
581
- * If you want array of records, use {@link parseString.toArray} function.
582
- * @example Parsing CSV files from strings
583
- *
584
- * ```ts
585
- * import { parseString } from 'web-csv-toolbox';
586
- *
587
- * const csv = `name,age
588
- * Alice,42
589
- * Bob,69`;
590
- *
591
- * for await (const record of parseString(csv)) {
592
- * console.log(record);
593
- * }
594
- * // Prints:
595
- * // { name: 'Alice', age: '42' }
596
- * // { name: 'Bob', age: '69' }
597
- * ```
598
- */
599
- export declare function parseString<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
600
-
601
- export declare namespace parseString {
602
- /**
603
- * Parse CSV string to records.
604
- *
605
- * @returns Array of records
606
- *
607
- * @example
608
- * ```ts
609
- * import { parseString } from 'web-csv-toolbox';
610
- *
611
- * const csv = `name,age
612
- * Alice,42
613
- * Bob,69`;
614
- *
615
- * const records = await parseString.toArray(csv);
616
- * console.log(records);
617
- * // Prints:
618
- * // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
619
- * ```
620
- */
621
- export function toArray<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): Promise<CSVRecord<Header>[]>;
622
- /**
623
- * Parse CSV string to records.
624
- *
625
- * @returns Array of records
626
- *
627
- * @example
628
- *
629
- * ```ts
630
- * import { parseString } from 'web-csv-toolbox';
631
- *
632
- * const csv = `name,age
633
- * Alice,42
634
- * Bob,69`;
635
- *
636
- * const records = parseString.toArraySync(csv);
637
- * console.log(records);
638
- * // Prints:
639
- * // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
640
- * ```
641
- */
642
- export function toArraySync<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): CSVRecord<Header>[];
643
- /**
644
- * Parse CSV string to records.
645
- *
646
- * @returns Async iterable iterator of records
647
- *
648
- * @example
649
- * ```ts
650
- * import { parseString } from 'web-csv-toolbox';
651
- *
652
- * const csv = `name,age
653
- * Alice,42
654
- * Bob,69`;
655
- *
656
- * for (const record of parseString.toIterableIterator(csv)) {
657
- * console.log(record);
658
- * }
659
- * // Prints:
660
- * // { name: 'Alice', age: '42' }
661
- * // { name: 'Bob', age: '69' }
662
- * ```
663
- */
664
- export function toIterableIterator<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): IterableIterator<CSVRecord<Header>>;
665
- /**
666
- * Parse CSV string to records.
667
- *
668
- * @returns Readable stream of records
669
- *
670
- * @example
671
- * ```ts
672
- * import { parseString } from 'web-csv-toolbox';
673
- *
674
- * const csv = `name,age
675
- * Alice,42
676
- * Bob,69`;
677
- *
678
- * await parseString.toStream(csv)
679
- * .pipeTo(
680
- * new WritableStream({
681
- * write(record) {
682
- * console.log(record);
683
- * },
684
- * }),
685
- * );
686
- * // Prints:
687
- * // { name: 'Alice', age: '42' }
688
- * // { name: 'Bob', age: '69' }
689
- * ```
690
- */
691
- export function toStream<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): ReadableStream<CSVRecord<Header>>;
692
- }
693
-
694
- /**
695
- * Parse CSV string stream to records.
696
- *
697
- * @category Middle-level API
698
- * @param stream CSV string stream to parse
699
- * @param options Parsing options.
700
- * @returns Async iterable iterator of records.
701
- *
702
- * If you want array of records, use {@link parseStringStream.toArray} function.
703
- *
704
- * @example Parsing CSV files from strings
705
- *
706
- * ```ts
707
- * import { parseStringStream } from 'web-csv-toolbox';
708
- *
709
- * const csv = `name,age
710
- * Alice,42
711
- * Bob,69`;
712
- *
713
- * const stream = new ReadableStream({
714
- * start(controller) {
715
- * controller.enqueue(csv);
716
- * controller.close();
717
- * },
718
- * });
719
- *
720
- * for await (const record of parseStringStream(csv)) {
721
- * console.log(record);
722
- * }
723
- * // Prints:
724
- * // { name: 'Alice', age: '42' }
725
- * // { name: 'Bob', age: '69' }
726
- * ```
727
- */
728
- export declare function parseStringStream<Header extends ReadonlyArray<string>>(stream: ReadableStream<string>, options?: ParseOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
729
-
730
- export declare namespace parseStringStream {
731
- /**
732
- * Parse CSV string stream to records.
733
- *
734
- * @returns Array of records
735
- *
736
- * @example
737
- *
738
- * ```ts
739
- * import { parseStringStream } from 'web-csv-toolbox';
740
- *
741
- * const csv = `name,age
742
- * Alice,42
743
- * Bob,69`;
744
- *
745
- * const stream = new ReadableStream({
746
- * start(controller) {
747
- * controller.enqueue(csv);
748
- * controller.close();
749
- * },
750
- * });
751
- *
752
- * const records = await parseStringStream.toArray(stream);
753
- * console.log(records);
754
- * // Prints:
755
- * // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
756
- * ```
757
- */
758
- export function toArray<Header extends ReadonlyArray<string>>(stream: ReadableStream<string>, options?: ParseOptions<Header>): Promise<CSVRecord<Header>[]>;
759
- /**
760
- * Parse CSV string stream to records.
761
- *
762
- * @returns Array of records
763
- *
764
- * @example
765
- *
766
- * ```ts
767
- * import { parseStringStream } from 'web-csv-toolbox';
768
- *
769
- * const csv = `name,age
770
- * Alice,42
771
- * Bob,69`;
772
- *
773
- * const stream = new ReadableStream({
774
- * start(controller) {
775
- * controller.enqueue(csv);
776
- * controller.close();
777
- * },
778
- * });
779
- *
780
- * await parseStringStream.toStream(stream)
781
- * .pipeTo(
782
- * new WritableStream({
783
- * write(record) {
784
- * console.log(record);
785
- * },
786
- * }),
787
- * );
788
- * ```
789
- */
790
- export function toStream<Header extends ReadonlyArray<string>>(stream: ReadableStream<string>, options?: ParseOptions<Header>): ReadableStream<CSVRecord<Header>>;
791
- }
792
-
793
- /**
794
- * Parse CSV to records.
795
- * This function is for parsing a binary stream.
796
- *
797
- * @category Middle-level API
798
- * @remarks
799
- * If you want to parse a string, use {@link parseStringStream}.
800
- * @param stream CSV string to parse
801
- * @param options Parsing options.
802
- * @returns Async iterable iterator of records.
803
- *
804
- * If you want array of records, use {@link parseUint8ArrayStream.toArray} function.
805
- *
806
- * @example Parsing CSV binary
807
- *
808
- * ```ts
809
- * import { parseUint8ArrayStream } from 'web-csv-toolbox';
810
- *
811
- * const csv = Uint8Array.from([
812
- * // ...
813
- * ]);
814
- *
815
- * const stream = new ReadableStream({
816
- * start(controller) {
817
- * controller.enqueue(csv);
818
- * controller.close();
819
- * },
820
- * });
821
- *
822
- * for await (const record of parseUint8ArrayStream(csv)) {
823
- * console.log(record);
824
- * }
825
- * ```
826
- */
827
- export declare function parseUint8ArrayStream<Header extends ReadonlyArray<string>>(stream: ReadableStream<Uint8Array>, options?: ParseBinaryOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
828
-
829
- export declare namespace parseUint8ArrayStream {
830
- /**
831
- * Parse CSV binary to array of records,
832
- * ideal for smaller data sets.
833
- *
834
- * @returns Array of records
835
- *
836
- * @example Parsing CSV binary
837
- * ```ts
838
- * import { parseUint8ArrayStream } from 'web-csv-toolbox';
839
- *
840
- * const csv = Uint8Array.from([
841
- * // ...
842
- * ]);
843
- *
844
- * const stream = new ReadableStream({
845
- * start(controller) {
846
- * controller.enqueue(csv);
847
- * controller.close();
848
- * },
849
- * });
850
- *
851
- * const records = await parseUint8ArrayStream.toArray(stream);
852
- * console.log(records);
853
- * ```
854
- */
855
- export function toArray<Header extends ReadonlyArray<string>>(stream: ReadableStream<Uint8Array>, options?: ParseBinaryOptions<Header>): Promise<CSVRecord<Header>[]>;
856
- /**
857
- * Parse CSV binary to array of records.
858
- *
859
- * @returns Stream of records
860
- *
861
- * @example Parsing CSV binary
862
- * ```ts
863
- * import { parseUint8ArrayStream } from 'web-csv-toolbox';
864
- *
865
- * const csv = Uint8Array.from([
866
- * // ...
867
- * ]);
868
- *
869
- * const stream = new ReadableStream({
870
- * start(controller) {
871
- * controller.enqueue(csv);
872
- * controller.close();
873
- * },
874
- * });
875
- *
876
- * await parseUint8ArrayStream.toStream(stream)
877
- * .pipeTo(new WritableStream({
878
- * write(record) {
879
- * console.log(record);
880
- * },
881
- * }),
882
- * );
883
- * ```
884
- */
885
- export function toStream<Header extends ReadonlyArray<string>>(stream: ReadableStream<Uint8Array>, options?: ParseBinaryOptions<Header>): ReadableStream<CSVRecord<Header>[]>;
886
- }
887
-
888
- /**
889
- * Record Assembler Options for CSV.
890
- * @category Types
891
- *
892
- * @remarks
893
- * If you specify `header: ['foo', 'bar']`,
894
- * the first record will be treated as a normal record.
895
- *
896
- * If you don't specify `header`,
897
- * the first record will be treated as a header.
898
- */
899
- export declare interface RecordAssemblerOptions<Header extends ReadonlyArray<string>> {
900
- /**
901
- * CSV header.
902
- *
903
- * @remarks
904
- * If you specify this option,
905
- * the first record will be treated as a normal record.
906
- *
907
- * If you don't specify this option,
908
- * the first record will be treated as a header.
909
- *
910
- * @default undefined
911
- */
912
- header?: Header;
913
- }
914
-
915
- /**
916
- * A transform stream that converts a stream of tokens into a stream of rows.
917
- * @template Header The type of the header row.
918
- * @param options The options for the parser.
919
- *
920
- * @category Low-level API
921
- *
922
- * @example Parse a CSV with headers by data
923
- * ```ts
924
- * new ReadableStream({
925
- * start(controller) {
926
- * controller.enqueue("name,age\r\n");
927
- * controller.enqueue("Alice,20\r\n");
928
- * controller.enqueue("Bob,25\r\n");
929
- * controller.enqueue("Charlie,30\r\n");
930
- * controller.close();
931
- * })
932
- * .pipeThrough(new LexerTransformer())
933
- * .pipeThrough(new RecordAssemblerTransformer())
934
- * .pipeTo(new WritableStream({ write(row) { console.log(row); }}));
935
- * // { name: "Alice", age: "20" }
936
- * // { name: "Bob", age: "25" }
937
- * // { name: "Charlie", age: "30" }
938
- * ```
939
- *
940
- * @example Parse a CSV with headers by options
941
- * ```ts
942
- * new ReadableStream({
943
- * start(controller) {
944
- * controller.enqueue("Alice,20\r\n");
945
- * controller.enqueue("Bob,25\r\n");
946
- * controller.enqueue("Charlie,30\r\n");
947
- * controller.close();
948
- * }
949
- * })
950
- * .pipeThrough(new LexerTransformer())
951
- * .pipeThrough(new RecordAssemblerTransformer({ header: ["name", "age"] }))
952
- * .pipeTo(new WritableStream({ write(row) { console.log(row); }}));
953
- * // { name: "Alice", age: "20" }
954
- * // { name: "Bob", age: "25" }
955
- * // { name: "Charlie", age: "30" }
956
- * ```
957
- */
958
- export declare class RecordAssemblerTransformer<Header extends ReadonlyArray<string>> extends TransformStream<Token[], CSVRecord<Header>> {
959
- constructor(options?: RecordAssemblerOptions<Header>);
960
- }
961
-
962
- /**
963
- * RecordDelimiter is a symbol for record delimiter of CSV.
964
- * @category Constants
965
- */
966
- export declare const RecordDelimiter: unique symbol;
967
-
968
- /**
969
- * Record delimiter token type.
970
- */
971
- export declare interface RecordDelimiterToken {
972
- type: typeof RecordDelimiter;
973
- }
974
-
975
- /**
976
- * Token is a atomic unit of a CSV file.
977
- * It can be a field, field delimiter, or record delimiter.
978
- * @category Types
979
- */
980
- export declare type Token = FieldToken | typeof FieldDelimiter | typeof RecordDelimiter;
981
-
982
- export { }