web-csv-toolbox 0.0.0-next-20240116142901 → 0.0.0-next-20240116161004

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.js +2 -0
  2. package/dist/cjs/Lexer.js.map +1 -0
  3. package/dist/cjs/LexerTransformer.js +2 -0
  4. package/dist/cjs/LexerTransformer.js.map +1 -0
  5. package/dist/cjs/RecordAssembler.js +2 -0
  6. package/dist/cjs/RecordAssembler.js.map +1 -0
  7. package/dist/cjs/RecordAssemblerTransformer.js +2 -0
  8. package/dist/cjs/RecordAssemblerTransformer.js.map +1 -0
  9. package/dist/cjs/assertCommonOptions.js +2 -0
  10. package/dist/cjs/assertCommonOptions.js.map +1 -0
  11. package/dist/cjs/common/constants.js +2 -0
  12. package/dist/cjs/common/constants.js.map +1 -0
  13. package/dist/cjs/constants.js +2 -0
  14. package/dist/cjs/constants.js.map +1 -0
  15. package/dist/cjs/convertBinaryToString.js +2 -0
  16. package/dist/cjs/convertBinaryToString.js.map +1 -0
  17. package/dist/cjs/getOptionsFromResponse.js +2 -0
  18. package/dist/cjs/getOptionsFromResponse.js.map +1 -0
  19. package/dist/cjs/parse.js +2 -0
  20. package/dist/cjs/parse.js.map +1 -0
  21. package/dist/cjs/parseBinary.js +2 -0
  22. package/dist/cjs/parseBinary.js.map +1 -0
  23. package/dist/cjs/parseBinaryToArraySync.js +2 -0
  24. package/dist/cjs/parseBinaryToArraySync.js.map +1 -0
  25. package/dist/cjs/parseBinaryToIterableIterator.js +2 -0
  26. package/dist/cjs/parseBinaryToIterableIterator.js.map +1 -0
  27. package/dist/cjs/parseBinaryToStream.js +2 -0
  28. package/dist/cjs/parseBinaryToStream.js.map +1 -0
  29. package/dist/cjs/parseResponse.js +2 -0
  30. package/dist/cjs/parseResponse.js.map +1 -0
  31. package/dist/cjs/parseResponseToStream.js +2 -0
  32. package/dist/cjs/parseResponseToStream.js.map +1 -0
  33. package/dist/cjs/parseString.js +2 -0
  34. package/dist/cjs/parseString.js.map +1 -0
  35. package/dist/cjs/parseStringStream.js +2 -0
  36. package/dist/cjs/parseStringStream.js.map +1 -0
  37. package/dist/cjs/parseStringStreamToStream.js +2 -0
  38. package/dist/cjs/parseStringStreamToStream.js.map +1 -0
  39. package/dist/cjs/parseStringToArraySync.js +2 -0
  40. package/dist/cjs/parseStringToArraySync.js.map +1 -0
  41. package/dist/cjs/parseStringToIterableIterator.js +2 -0
  42. package/dist/cjs/parseStringToIterableIterator.js.map +1 -0
  43. package/dist/cjs/parseStringToStream.js +2 -0
  44. package/dist/cjs/parseStringToStream.js.map +1 -0
  45. package/dist/cjs/parseUint8ArrayStream.js +2 -0
  46. package/dist/cjs/parseUint8ArrayStream.js.map +1 -0
  47. package/dist/cjs/parseUint8ArrayStreamToStream.js +2 -0
  48. package/dist/cjs/parseUint8ArrayStreamToStream.js.map +1 -0
  49. package/dist/cjs/utils/convertIterableIteratorToAsync.js +2 -0
  50. package/dist/cjs/utils/convertIterableIteratorToAsync.js.map +1 -0
  51. package/dist/cjs/utils/convertStreamToAsyncIterableIterator.js +2 -0
  52. package/dist/cjs/utils/convertStreamToAsyncIterableIterator.js.map +1 -0
  53. package/dist/cjs/utils/convertThisAsyncIterableIteratorToArray.js +2 -0
  54. package/dist/cjs/utils/convertThisAsyncIterableIteratorToArray.js.map +1 -0
  55. package/dist/cjs/utils/escapeRegExp.js +2 -0
  56. package/dist/cjs/utils/escapeRegExp.js.map +1 -0
  57. package/dist/cjs/utils/parseMime.js +2 -0
  58. package/dist/cjs/utils/parseMime.js.map +1 -0
  59. package/dist/cjs/utils/pipeline.js +2 -0
  60. package/dist/cjs/utils/pipeline.js.map +1 -0
  61. package/dist/cjs/web-csv-toolbox.js +2 -0
  62. package/dist/cjs/web-csv-toolbox.js.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,576 +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
-
5
- const CR = "\r";
6
- const CRLF = "\r\n";
7
- const LF = "\n";
8
- const COMMA = ",";
9
- const DOUBLE_QUOTE = '"';
10
-
11
- function assertCommonOptions(options) {
12
- if (typeof options.quotation === "string" && options.quotation.length === 0) {
13
- throw new Error("quotation must not be empty");
14
- }
15
- if (typeof options.delimiter === "string" && options.delimiter.length === 0) {
16
- throw new Error("delimiter must not be empty");
17
- }
18
- if (options.quotation.includes(LF) || options.quotation.includes(CR)) {
19
- throw new Error("quotation must not include CR or LF");
20
- }
21
- if (options.delimiter.includes(LF) || options.delimiter.includes(CR)) {
22
- throw new Error("delimiter must not include CR or LF");
23
- }
24
- if (options.delimiter.includes(options.quotation) || options.quotation.includes(options.delimiter)) {
25
- throw new Error(
26
- "delimiter and quotation must not include each other as a substring"
27
- );
28
- }
29
- }
30
-
31
- function escapeRegExp(v) {
32
- return v.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
33
- }
34
-
35
- class Lexer {
36
- #delimiter;
37
- #delimiterLength;
38
- #quotation;
39
- #quotationLength;
40
- #matcher;
41
- #buffer = "";
42
- #flush = false;
43
- constructor({
44
- delimiter = COMMA,
45
- quotation = DOUBLE_QUOTE
46
- } = {}) {
47
- assertCommonOptions({ delimiter, quotation });
48
- this.#delimiter = delimiter;
49
- this.#delimiterLength = delimiter.length;
50
- this.#quotation = quotation;
51
- this.#quotationLength = quotation.length;
52
- const d = escapeRegExp(delimiter);
53
- const q = escapeRegExp(quotation);
54
- this.#matcher = new RegExp(
55
- `^(?:(?!${q})(?!${d})(?![\\r\\n]))([\\S\\s\\uFEFF\\xA0]+?)(?=${q}|${d}|\\r|\\n|$)`
56
- );
57
- }
58
- lex(chunk, buffering = false) {
59
- if (!buffering) {
60
- this.#flush = true;
61
- }
62
- if (typeof chunk === "string" && chunk.length !== 0) {
63
- this.#buffer += chunk;
64
- }
65
- return this.#tokens();
66
- }
67
- flush() {
68
- this.#flush = true;
69
- return [...this.#tokens()];
70
- }
71
- *#tokens() {
72
- if (this.#flush) {
73
- if (this.#buffer.endsWith(CRLF)) {
74
- this.#buffer = this.#buffer.slice(0, -CRLF.length);
75
- } else if (this.#buffer.endsWith(LF)) {
76
- this.#buffer = this.#buffer.slice(0, -LF.length);
77
- }
78
- }
79
- let currentField = null;
80
- for (let token; token = this.#nextToken(); ) {
81
- switch (token) {
82
- case FieldDelimiter:
83
- if (currentField) {
84
- yield currentField;
85
- currentField = null;
86
- }
87
- yield token;
88
- break;
89
- case RecordDelimiter:
90
- if (currentField) {
91
- yield currentField;
92
- currentField = null;
93
- }
94
- yield token;
95
- break;
96
- default:
97
- if (currentField) {
98
- currentField.value += token.value;
99
- } else {
100
- currentField = token;
101
- }
102
- break;
103
- }
104
- }
105
- if (currentField) {
106
- yield currentField;
107
- }
108
- }
109
- #nextToken() {
110
- if (this.#buffer.length === 0) {
111
- return null;
112
- }
113
- if (this.#flush === false && (this.#buffer === CRLF || this.#buffer === LF)) {
114
- return null;
115
- }
116
- if (this.#buffer.startsWith(CRLF)) {
117
- this.#buffer = this.#buffer.slice(2);
118
- return RecordDelimiter;
119
- }
120
- if (this.#buffer.startsWith(LF)) {
121
- this.#buffer = this.#buffer.slice(1);
122
- return RecordDelimiter;
123
- }
124
- if (this.#buffer.startsWith(this.#delimiter)) {
125
- this.#buffer = this.#buffer.slice(this.#delimiterLength);
126
- return FieldDelimiter;
127
- }
128
- if (this.#buffer.startsWith(this.#quotation)) {
129
- if (this.#flush === false && this.#buffer.endsWith(this.#quotation)) {
130
- return null;
131
- }
132
- return this.#extractQuotedString();
133
- }
134
- const match = this.#matcher.exec(this.#buffer);
135
- if (match) {
136
- if (this.#flush === false && match[0].length === this.#buffer.length) {
137
- return null;
138
- }
139
- this.#buffer = this.#buffer.slice(match[0].length);
140
- return { type: Field, value: match[0] };
141
- }
142
- return null;
143
- }
144
- #extractQuotedString() {
145
- let end = this.#quotationLength;
146
- let value = "";
147
- while (end < this.#buffer.length) {
148
- if (this.#buffer.slice(end, end + this.#quotationLength) === this.#quotation && this.#buffer.slice(
149
- end + this.#quotationLength,
150
- end + this.#quotationLength * 2
151
- ) === this.#quotation) {
152
- value += this.#quotation;
153
- end += this.#quotationLength * 2;
154
- continue;
155
- }
156
- if (this.#buffer.slice(end, end + this.#quotationLength) === this.#quotation) {
157
- if (this.#flush === false && end + this.#quotationLength < this.#buffer.length && this.#buffer.slice(
158
- end + this.#quotationLength,
159
- this.#delimiterLength
160
- ) !== this.#delimiter && this.#buffer.slice(
161
- end + this.#quotationLength,
162
- end + this.#quotationLength + 2
163
- ) !== CRLF && this.#buffer.slice(
164
- end + this.#quotationLength,
165
- end + this.#quotationLength + 1
166
- ) !== LF) {
167
- return null;
168
- }
169
- this.#buffer = this.#buffer.slice(end + this.#quotationLength);
170
- return { type: Field, value };
171
- }
172
- value += this.#buffer[end];
173
- end++;
174
- }
175
- return null;
176
- }
177
- }
178
-
179
- class LexerTransformer extends TransformStream {
180
- constructor(options = {}) {
181
- const lexer = new Lexer(options);
182
- super({
183
- transform: (chunk, controller) => {
184
- if (chunk.length !== 0) {
185
- controller.enqueue([...lexer.lex(chunk, true)]);
186
- }
187
- },
188
- flush: (controller) => {
189
- controller.enqueue(lexer.flush());
190
- }
191
- });
192
- }
193
- }
194
-
195
- class RecordAssembler {
196
- #fieldIndex = 0;
197
- #row = [];
198
- #header;
199
- #dirty = false;
200
- constructor(options = {}) {
201
- if (options.header !== void 0 && Array.isArray(options.header)) {
202
- this.#setHeader(options.header);
203
- }
204
- }
205
- *assemble(tokens, flush = true) {
206
- for (const token of tokens) {
207
- switch (token) {
208
- case FieldDelimiter:
209
- this.#fieldIndex++;
210
- this.#dirty = true;
211
- break;
212
- case RecordDelimiter:
213
- if (this.#header === void 0) {
214
- this.#setHeader(this.#row);
215
- } else {
216
- if (this.#dirty) {
217
- yield Object.fromEntries(
218
- this.#header.map((header, index) => [
219
- header,
220
- this.#row.at(index)
221
- ])
222
- );
223
- } else {
224
- yield Object.fromEntries(
225
- this.#header.map((header) => [header, ""])
226
- );
227
- }
228
- }
229
- this.#fieldIndex = 0;
230
- this.#row = new Array(this.#header?.length).fill("");
231
- this.#dirty = false;
232
- break;
233
- default:
234
- this.#dirty = true;
235
- this.#row[this.#fieldIndex] = token.value;
236
- break;
237
- }
238
- }
239
- if (flush) {
240
- yield* this.flush();
241
- }
242
- }
243
- *flush() {
244
- if (this.#header !== void 0) {
245
- if (this.#dirty) {
246
- yield Object.fromEntries(
247
- this.#header.filter((v) => v).map((header, index) => [header, this.#row.at(index)])
248
- );
249
- }
250
- }
251
- }
252
- #setHeader(header) {
253
- this.#header = header;
254
- if (this.#header.length === 0) {
255
- throw new Error("The header must not be empty.");
256
- }
257
- if (new Set(this.#header).size !== this.#header.length) {
258
- throw new Error("The header must not contain duplicate fields.");
259
- }
260
- }
261
- }
262
-
263
- class RecordAssemblerTransformer extends TransformStream {
264
- constructor(options = {}) {
265
- const assembler = new RecordAssembler(options);
266
- super({
267
- transform: (tokens, controller) => {
268
- for (const token of assembler.assemble(tokens, false)) {
269
- controller.enqueue(token);
270
- }
271
- },
272
- flush: (controller) => {
273
- for (const token of assembler.flush()) {
274
- controller.enqueue(token);
275
- }
276
- }
277
- });
278
- }
279
- }
280
-
281
- function parseStringToArraySync(csv, options) {
282
- const lexer = new Lexer(options);
283
- const assembler = new RecordAssembler(options);
284
- const tokens = lexer.lex(csv);
285
- return [...assembler.assemble(tokens)];
286
- }
287
-
288
- function parseStringToIterableIterator(csv, options) {
289
- const lexer = new Lexer(options);
290
- const assembler = new RecordAssembler(options);
291
- const tokens = lexer.lex(csv);
292
- return assembler.assemble(tokens);
293
- }
294
-
295
- function parseStringToStream(csv, options) {
296
- const lexer = new Lexer(options);
297
- const assembler = new RecordAssembler(options);
298
- return new ReadableStream({
299
- start(controller) {
300
- const tokens = lexer.lex(csv);
301
- for (const record of assembler.assemble(tokens)) {
302
- controller.enqueue(record);
303
- }
304
- controller.close();
305
- }
306
- });
307
- }
308
-
309
- async function toArray(...args) {
310
- const rows = [];
311
- for await (const row of this(...args)) {
312
- rows.push(row);
313
- }
314
- return rows;
315
- }
316
-
317
- async function* parseString(csv, options) {
318
- yield* parseStringToIterableIterator(csv, options);
319
- }
320
- ((parseString2) => {
321
- Object.defineProperty(parseString2, "toArray", {
322
- enumerable: true,
323
- writable: false,
324
- value: toArray
325
- });
326
- Object.defineProperty(parseString2, "toArraySync", {
327
- enumerable: true,
328
- writable: false,
329
- value: parseStringToArraySync
330
- });
331
- Object.defineProperty(parseString2, "toIterableIterator", {
332
- enumerable: true,
333
- writable: false,
334
- value: parseStringToIterableIterator
335
- });
336
- Object.defineProperty(parseString2, "toStream", {
337
- enumerable: true,
338
- writable: false,
339
- value: parseStringToStream
340
- });
341
- })(parseString || (parseString = {}));
342
-
343
- function convertBinaryToString(binary, options) {
344
- return new TextDecoder(options?.charset, {
345
- ignoreBOM: options?.ignoreBOM,
346
- fatal: options?.fatal
347
- }).decode(binary instanceof ArrayBuffer ? new Uint8Array(binary) : binary);
348
- }
349
-
350
- function parseBinaryToArraySync(binary, options = {}) {
351
- const csv = convertBinaryToString(binary, options);
352
- return parseStringToArraySync(csv, options);
353
- }
354
-
355
- function parseBinaryToIterableIterator(binary, options = {}) {
356
- const csv = convertBinaryToString(binary, options);
357
- return parseStringToIterableIterator(csv, options);
358
- }
359
-
360
- function parseBinaryToStream(binary, options = {}) {
361
- const csv = convertBinaryToString(binary, options);
362
- return parseStringToStream(csv, options);
363
- }
364
-
365
- function iterableIteratorToAsync(iterator) {
366
- return {
367
- async next() {
368
- const result = iterator.next();
369
- return Promise.resolve(result);
370
- },
371
- [Symbol.asyncIterator]() {
372
- return this;
373
- }
374
- };
375
- }
376
-
377
- function parseBinary(bytes, options) {
378
- const iterator = parseBinaryToIterableIterator(bytes, options);
379
- return iterableIteratorToAsync(iterator);
380
- }
381
- ((parseBinary2) => {
382
- Object.defineProperty(parseBinary2, "toArray", {
383
- enumerable: true,
384
- writable: false,
385
- value: toArray
386
- });
387
- Object.defineProperty(parseBinary2, "toArraySync", {
388
- enumerable: true,
389
- writable: false,
390
- value: parseBinaryToArraySync
391
- });
392
- Object.defineProperty(parseBinary2, "toIterableIterator", {
393
- enumerable: true,
394
- writable: false,
395
- value: parseBinaryToIterableIterator
396
- });
397
- Object.defineProperty(parseBinary2, "toStream", {
398
- enumerable: true,
399
- writable: false,
400
- value: parseBinaryToStream
401
- });
402
- })(parseBinary || (parseBinary = {}));
403
-
404
- function pipeline(stream, ...transformers) {
405
- return new ReadableStream({
406
- start: (controller) => {
407
- (() => transformers.reduce(
408
- (stream2, transformer) => stream2.pipeThrough(transformer),
409
- stream
410
- ).pipeTo(
411
- new WritableStream({
412
- write: (v) => controller.enqueue(v),
413
- close: () => controller.close()
414
- })
415
- ))();
416
- }
417
- });
418
- }
419
-
420
- function parseUint8ArrayStreamToStream(stream, options) {
421
- const { charset, fatal, ignoreBOM, decomposition } = options ?? {};
422
- return decomposition ? pipeline(
423
- stream,
424
- new DecompressionStream(decomposition),
425
- new TextDecoderStream(charset, { fatal, ignoreBOM }),
426
- new LexerTransformer(options),
427
- new RecordAssemblerTransformer(options)
428
- ) : pipeline(
429
- stream,
430
- new TextDecoderStream(charset, { fatal, ignoreBOM }),
431
- new LexerTransformer(options),
432
- new RecordAssemblerTransformer(options)
433
- );
434
- }
435
-
436
- async function* streamToAsyncIterableIterator(stream) {
437
- const reader = stream.getReader();
438
- while (true) {
439
- const { done, value } = await reader.read();
440
- if (done)
441
- break;
442
- yield value;
443
- }
444
- }
445
-
446
- function parseUint8ArrayStream(stream, options) {
447
- const recordStream = parseUint8ArrayStreamToStream(stream, options);
448
- return streamToAsyncIterableIterator(recordStream);
449
- }
450
- ((parseUint8ArrayStream2) => {
451
- Object.defineProperty(parseUint8ArrayStream2, "toArray", {
452
- enumerable: true,
453
- writable: false,
454
- value: toArray
455
- });
456
- Object.defineProperty(parseUint8ArrayStream2, "toStream", {
457
- enumerable: true,
458
- writable: false,
459
- value: parseUint8ArrayStreamToStream
460
- });
461
- })(parseUint8ArrayStream || (parseUint8ArrayStream = {}));
462
-
463
- function parseStringStreamToStream(stream, options) {
464
- return pipeline(
465
- stream,
466
- new LexerTransformer(options),
467
- new RecordAssemblerTransformer(options)
468
- );
469
- }
470
-
471
- function parseStringStream(stream, options) {
472
- const recordStream = parseStringStreamToStream(stream, options);
473
- return streamToAsyncIterableIterator(recordStream);
474
- }
475
- ((parseStringStream2) => {
476
- Object.defineProperty(parseStringStream2, "toArray", {
477
- enumerable: true,
478
- writable: false,
479
- value: toArray
480
- });
481
- Object.defineProperty(parseStringStream2, "toStream", {
482
- enumerable: true,
483
- writable: false,
484
- value: parseStringStreamToStream
485
- });
486
- })(parseStringStream || (parseStringStream = {}));
487
-
488
- function parseMime(contentType) {
489
- const [type, ...parameters] = contentType.split(";");
490
- const result = {
491
- type: type.trim(),
492
- parameters: {}
493
- };
494
- for (const paramator of parameters) {
495
- const [key, value] = paramator.split("=");
496
- result.parameters[key.trim()] = value.trim();
497
- }
498
- return result;
499
- }
500
-
501
- function getOptionsFromResponse(response, options = {}) {
502
- const { headers } = response;
503
- const contentType = headers.get("content-type") ?? "text/csv";
504
- const mime = parseMime(contentType);
505
- if (mime.type !== "text/csv") {
506
- throw new Error(`Invalid mime type: ${contentType}`);
507
- }
508
- const decomposition = headers.get("content-encoding") ?? void 0;
509
- const charset = mime.parameters.charset ?? "utf-8";
510
- return {
511
- decomposition,
512
- charset,
513
- ...options
514
- };
515
- }
516
-
517
- function parseResponseToStream(response, options) {
518
- const options_ = getOptionsFromResponse(response, options);
519
- if (response.body === null) {
520
- throw new Error("Response body is null");
521
- }
522
- return parseUint8ArrayStreamToStream(response.body, options_);
523
- }
524
-
525
- function parseResponse(response, options) {
526
- const options_ = getOptionsFromResponse(response, options);
527
- if (response.body === null) {
528
- throw new Error("Response body is null");
529
- }
530
- return parseUint8ArrayStream(response.body, options_);
531
- }
532
- ((parseResponse2) => {
533
- Object.defineProperty(parseResponse2, "toArray", {
534
- enumerable: true,
535
- writable: false,
536
- value: toArray
537
- });
538
- Object.defineProperty(parseResponse2, "toStream", {
539
- enumerable: true,
540
- writable: false,
541
- value: parseResponseToStream
542
- });
543
- })(parseResponse || (parseResponse = {}));
544
-
545
- async function* parse(csv, options) {
546
- if (typeof csv === "string") {
547
- yield* parseString(csv, options);
548
- } else if (csv instanceof Uint8Array || csv instanceof ArrayBuffer) {
549
- yield* parseBinary(csv, options);
550
- } else if (csv instanceof ReadableStream) {
551
- const [branch1, branch2] = csv.tee();
552
- const reader1 = branch1.getReader();
553
- const { value: firstChunk } = await reader1.read();
554
- reader1.releaseLock();
555
- if (typeof firstChunk === "string") {
556
- yield* parseStringStream(branch2, options);
557
- } else if (firstChunk instanceof Uint8Array) {
558
- yield* parseUint8ArrayStream(
559
- branch2,
560
- options
561
- );
562
- }
563
- } else if (csv instanceof Response) {
564
- yield* parseResponse(csv, options);
565
- }
566
- }
567
- ((parse2) => {
568
- Object.defineProperty(parse2, "toArray", {
569
- enumerable: true,
570
- writable: false,
571
- value: toArray
572
- });
573
- })(parse || (parse = {}));
574
-
575
- export { Field, FieldDelimiter, LexerTransformer, RecordAssemblerTransformer, RecordDelimiter, parse, parseBinary, parseResponse, parseString, parseStringStream, parseUint8ArrayStream };
576
- //# sourceMappingURL=web-csv-toolbox.js.map