web-csv-toolbox 0.5.2 → 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.
- package/README.md +2 -2
- package/dist/cjs/Lexer.cjs +2 -0
- package/dist/cjs/Lexer.cjs.map +1 -0
- package/dist/cjs/LexerTransformer.cjs +2 -0
- package/dist/cjs/LexerTransformer.cjs.map +1 -0
- package/dist/cjs/RecordAssembler.cjs +2 -0
- package/dist/cjs/RecordAssembler.cjs.map +1 -0
- package/dist/cjs/RecordAssemblerTransformer.cjs +2 -0
- package/dist/cjs/RecordAssemblerTransformer.cjs.map +1 -0
- package/dist/cjs/assertCommonOptions.cjs +2 -0
- package/dist/cjs/assertCommonOptions.cjs.map +1 -0
- package/dist/cjs/common/constants.cjs +2 -0
- package/dist/cjs/common/constants.cjs.map +1 -0
- package/dist/cjs/constants.cjs +2 -0
- package/dist/cjs/constants.cjs.map +1 -0
- package/dist/cjs/convertBinaryToString.cjs +2 -0
- package/dist/cjs/convertBinaryToString.cjs.map +1 -0
- package/dist/cjs/getOptionsFromResponse.cjs +2 -0
- package/dist/cjs/getOptionsFromResponse.cjs.map +1 -0
- package/dist/cjs/parse.cjs +2 -0
- package/dist/cjs/parse.cjs.map +1 -0
- package/dist/cjs/parseBinary.cjs +2 -0
- package/dist/cjs/parseBinary.cjs.map +1 -0
- package/dist/cjs/parseBinaryToArraySync.cjs +2 -0
- package/dist/cjs/parseBinaryToArraySync.cjs.map +1 -0
- package/dist/cjs/parseBinaryToIterableIterator.cjs +2 -0
- package/dist/cjs/parseBinaryToIterableIterator.cjs.map +1 -0
- package/dist/cjs/parseBinaryToStream.cjs +2 -0
- package/dist/cjs/parseBinaryToStream.cjs.map +1 -0
- package/dist/cjs/parseResponse.cjs +2 -0
- package/dist/cjs/parseResponse.cjs.map +1 -0
- package/dist/cjs/parseResponseToStream.cjs +2 -0
- package/dist/cjs/parseResponseToStream.cjs.map +1 -0
- package/dist/cjs/parseString.cjs +2 -0
- package/dist/cjs/parseString.cjs.map +1 -0
- package/dist/cjs/parseStringStream.cjs +2 -0
- package/dist/cjs/parseStringStream.cjs.map +1 -0
- package/dist/cjs/parseStringStreamToStream.cjs +2 -0
- package/dist/cjs/parseStringStreamToStream.cjs.map +1 -0
- package/dist/cjs/parseStringToArraySync.cjs +2 -0
- package/dist/cjs/parseStringToArraySync.cjs.map +1 -0
- package/dist/cjs/parseStringToIterableIterator.cjs +2 -0
- package/dist/cjs/parseStringToIterableIterator.cjs.map +1 -0
- package/dist/cjs/parseStringToStream.cjs +2 -0
- package/dist/cjs/parseStringToStream.cjs.map +1 -0
- package/dist/cjs/parseUint8ArrayStream.cjs +2 -0
- package/dist/cjs/parseUint8ArrayStream.cjs.map +1 -0
- package/dist/cjs/parseUint8ArrayStreamToStream.cjs +2 -0
- package/dist/cjs/parseUint8ArrayStreamToStream.cjs.map +1 -0
- package/dist/cjs/utils/convertIterableIteratorToAsync.cjs +2 -0
- package/dist/cjs/utils/convertIterableIteratorToAsync.cjs.map +1 -0
- package/dist/cjs/utils/convertStreamToAsyncIterableIterator.cjs +2 -0
- package/dist/cjs/utils/convertStreamToAsyncIterableIterator.cjs.map +1 -0
- package/dist/cjs/utils/convertThisAsyncIterableIteratorToArray.cjs +2 -0
- package/dist/cjs/utils/convertThisAsyncIterableIteratorToArray.cjs.map +1 -0
- package/dist/cjs/utils/escapeRegExp.cjs +2 -0
- package/dist/cjs/utils/escapeRegExp.cjs.map +1 -0
- package/dist/cjs/utils/parseMime.cjs +2 -0
- package/dist/cjs/utils/parseMime.cjs.map +1 -0
- package/dist/cjs/utils/pipeline.cjs +2 -0
- package/dist/cjs/utils/pipeline.cjs.map +1 -0
- package/dist/cjs/web-csv-toolbox.cjs +2 -0
- package/dist/cjs/web-csv-toolbox.cjs.map +1 -0
- package/dist/es/Lexer.js +151 -0
- package/dist/es/Lexer.js.map +1 -0
- package/dist/es/LexerTransformer.js +20 -0
- package/dist/es/LexerTransformer.js.map +1 -0
- package/dist/es/RecordAssembler.js +72 -0
- package/dist/es/RecordAssembler.js.map +1 -0
- package/dist/es/RecordAssemblerTransformer.js +22 -0
- package/dist/es/RecordAssemblerTransformer.js.map +1 -0
- package/dist/es/assertCommonOptions.js +24 -0
- package/dist/es/assertCommonOptions.js.map +1 -0
- package/dist/es/common/constants.js +6 -0
- package/dist/es/common/constants.js.map +1 -0
- package/dist/es/constants.js +8 -0
- package/dist/es/constants.js.map +1 -0
- package/dist/es/convertBinaryToString.js +9 -0
- package/dist/es/convertBinaryToString.js.map +1 -0
- package/dist/es/getOptionsFromResponse.js +20 -0
- package/dist/es/getOptionsFromResponse.js.map +1 -0
- package/dist/es/parse.js +39 -0
- package/dist/es/parse.js.map +1 -0
- package/dist/es/parseBinary.js +35 -0
- package/dist/es/parseBinary.js.map +1 -0
- package/dist/es/parseBinaryToArraySync.js +10 -0
- package/dist/es/parseBinaryToArraySync.js.map +1 -0
- package/dist/es/parseBinaryToIterableIterator.js +10 -0
- package/dist/es/parseBinaryToIterableIterator.js.map +1 -0
- package/dist/es/parseBinaryToStream.js +10 -0
- package/dist/es/parseBinaryToStream.js.map +1 -0
- package/dist/es/parseResponse.js +27 -0
- package/dist/es/parseResponse.js.map +1 -0
- package/dist/es/parseResponseToStream.js +13 -0
- package/dist/es/parseResponseToStream.js.map +1 -0
- package/dist/es/parseString.js +33 -0
- package/dist/es/parseString.js.map +1 -0
- package/dist/es/parseStringStream.js +23 -0
- package/dist/es/parseStringStream.js.map +1 -0
- package/dist/es/parseStringStreamToStream.js +14 -0
- package/dist/es/parseStringStreamToStream.js.map +1 -0
- package/dist/es/parseStringToArraySync.js +12 -0
- package/dist/es/parseStringToArraySync.js.map +1 -0
- package/dist/es/parseStringToIterableIterator.js +12 -0
- package/dist/es/parseStringToIterableIterator.js.map +1 -0
- package/dist/es/parseStringToStream.js +19 -0
- package/dist/es/parseStringToStream.js.map +1 -0
- package/dist/es/parseUint8ArrayStream.js +23 -0
- package/dist/es/parseUint8ArrayStream.js.map +1 -0
- package/dist/es/parseUint8ArrayStreamToStream.js +22 -0
- package/dist/es/parseUint8ArrayStreamToStream.js.map +1 -0
- package/dist/es/utils/convertIterableIteratorToAsync.js +14 -0
- package/dist/es/utils/convertIterableIteratorToAsync.js.map +1 -0
- package/dist/es/utils/convertStreamToAsyncIterableIterator.js +12 -0
- package/dist/es/utils/convertStreamToAsyncIterableIterator.js.map +1 -0
- package/dist/es/utils/convertThisAsyncIterableIteratorToArray.js +10 -0
- package/dist/es/utils/convertThisAsyncIterableIteratorToArray.js.map +1 -0
- package/dist/es/utils/escapeRegExp.js +6 -0
- package/dist/es/utils/escapeRegExp.js.map +1 -0
- package/dist/es/utils/parseMime.js +15 -0
- package/dist/es/utils/parseMime.js.map +1 -0
- package/dist/es/utils/pipeline.js +18 -0
- package/dist/es/utils/pipeline.js.map +1 -0
- package/dist/es/web-csv-toolbox.js +10 -0
- package/dist/es/web-csv-toolbox.js.map +1 -0
- package/dist/types/Lexer.d.ts +7 -0
- package/dist/types/LexerTransformer.d.ts +34 -0
- package/dist/types/RecordAssembler.d.ts +7 -0
- package/dist/types/RecordAssemblerTransformer.d.ts +47 -0
- package/dist/types/assertCommonOptions.d.ts +7 -0
- package/dist/types/common/constants.d.ts +15 -0
- package/dist/types/common/types.d.ts +163 -0
- package/dist/types/constants.d.ts +11 -0
- package/dist/types/convertBinaryToString.d.ts +2 -0
- package/dist/types/escapeField.d.ts +13 -0
- package/dist/types/getOptionsFromResponse.d.ts +2 -0
- package/dist/types/parse.d.ts +182 -0
- package/dist/types/parseBinary.d.ts +113 -0
- package/dist/types/parseBinaryToArraySync.d.ts +2 -0
- package/dist/types/parseBinaryToIterableIterator.d.ts +2 -0
- package/dist/types/parseBinaryToStream.d.ts +2 -0
- package/dist/types/parseResponse.d.ts +80 -0
- package/dist/types/parseResponseToStream.d.ts +2 -0
- package/dist/types/parseString.d.ts +120 -0
- package/dist/types/parseStringStream.d.ts +98 -0
- package/dist/types/parseStringStreamToStream.d.ts +2 -0
- package/dist/types/parseStringToArraySync.d.ts +2 -0
- package/dist/types/parseStringToIterableIterator.d.ts +2 -0
- package/dist/types/parseStringToStream.d.ts +2 -0
- package/dist/types/parseUint8ArrayStream.d.ts +94 -0
- package/dist/types/parseUint8ArrayStreamToStream.d.ts +2 -0
- package/dist/types/utils/SingleValueReadableStream.d.ts +3 -0
- package/dist/types/utils/convertIterableIteratorToAsync.d.ts +1 -0
- package/dist/types/utils/convertStreamToAsyncIterableIterator.d.ts +1 -0
- package/dist/types/utils/convertThisAsyncIterableIteratorToArray.d.ts +1 -0
- package/dist/types/utils/escapeRegExp.d.ts +8 -0
- package/dist/types/utils/occurrences.d.ts +7 -0
- package/dist/types/utils/parseMime.d.ts +7 -0
- package/dist/types/utils/pipeline.d.ts +3 -0
- package/dist/types/web-csv-toolbox.d.ts +10 -0
- package/dist/web-csv-toolbox.umd.cjs +1 -1
- package/dist/web-csv-toolbox.umd.cjs.map +1 -1
- package/package.json +31 -13
- package/dist/web-csv-toolbox.cjs +0 -2
- package/dist/web-csv-toolbox.cjs.map +0 -1
- package/dist/web-csv-toolbox.d.ts +0 -982
- package/dist/web-csv-toolbox.js +0 -576
- package/dist/web-csv-toolbox.js.map +0 -1
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { CSVRecord, ParseOptions } from "./common/types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Parse CSV string to records.
|
|
4
|
+
*
|
|
5
|
+
* @category Middle-level API
|
|
6
|
+
* @param csv CSV string to parse
|
|
7
|
+
* @param options Parsing options. See {@link ParseOptions}.
|
|
8
|
+
* @returns Async iterable iterator of records.
|
|
9
|
+
*
|
|
10
|
+
* If you want array of records, use {@link parseString.toArray} function.
|
|
11
|
+
* @example Parsing CSV files from strings
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { parseString } from 'web-csv-toolbox';
|
|
15
|
+
*
|
|
16
|
+
* const csv = `name,age
|
|
17
|
+
* Alice,42
|
|
18
|
+
* Bob,69`;
|
|
19
|
+
*
|
|
20
|
+
* for await (const record of parseString(csv)) {
|
|
21
|
+
* console.log(record);
|
|
22
|
+
* }
|
|
23
|
+
* // Prints:
|
|
24
|
+
* // { name: 'Alice', age: '42' }
|
|
25
|
+
* // { name: 'Bob', age: '69' }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function parseString<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
|
|
29
|
+
export declare namespace parseString {
|
|
30
|
+
/**
|
|
31
|
+
* Parse CSV string to records.
|
|
32
|
+
*
|
|
33
|
+
* @returns Array of records
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { parseString } from 'web-csv-toolbox';
|
|
38
|
+
*
|
|
39
|
+
* const csv = `name,age
|
|
40
|
+
* Alice,42
|
|
41
|
+
* Bob,69`;
|
|
42
|
+
*
|
|
43
|
+
* const records = await parseString.toArray(csv);
|
|
44
|
+
* console.log(records);
|
|
45
|
+
* // Prints:
|
|
46
|
+
* // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
function toArray<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): Promise<CSVRecord<Header>[]>;
|
|
50
|
+
/**
|
|
51
|
+
* Parse CSV string to records.
|
|
52
|
+
*
|
|
53
|
+
* @returns Array of records
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
*
|
|
57
|
+
* ```ts
|
|
58
|
+
* import { parseString } from 'web-csv-toolbox';
|
|
59
|
+
*
|
|
60
|
+
* const csv = `name,age
|
|
61
|
+
* Alice,42
|
|
62
|
+
* Bob,69`;
|
|
63
|
+
*
|
|
64
|
+
* const records = parseString.toArraySync(csv);
|
|
65
|
+
* console.log(records);
|
|
66
|
+
* // Prints:
|
|
67
|
+
* // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
function toArraySync<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): CSVRecord<Header>[];
|
|
71
|
+
/**
|
|
72
|
+
* Parse CSV string to records.
|
|
73
|
+
*
|
|
74
|
+
* @returns Async iterable iterator of records
|
|
75
|
+
*
|
|
76
|
+
* @example
|
|
77
|
+
* ```ts
|
|
78
|
+
* import { parseString } from 'web-csv-toolbox';
|
|
79
|
+
*
|
|
80
|
+
* const csv = `name,age
|
|
81
|
+
* Alice,42
|
|
82
|
+
* Bob,69`;
|
|
83
|
+
*
|
|
84
|
+
* for (const record of parseString.toIterableIterator(csv)) {
|
|
85
|
+
* console.log(record);
|
|
86
|
+
* }
|
|
87
|
+
* // Prints:
|
|
88
|
+
* // { name: 'Alice', age: '42' }
|
|
89
|
+
* // { name: 'Bob', age: '69' }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
function toIterableIterator<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): IterableIterator<CSVRecord<Header>>;
|
|
93
|
+
/**
|
|
94
|
+
* Parse CSV string to records.
|
|
95
|
+
*
|
|
96
|
+
* @returns Readable stream of records
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```ts
|
|
100
|
+
* import { parseString } from 'web-csv-toolbox';
|
|
101
|
+
*
|
|
102
|
+
* const csv = `name,age
|
|
103
|
+
* Alice,42
|
|
104
|
+
* Bob,69`;
|
|
105
|
+
*
|
|
106
|
+
* await parseString.toStream(csv)
|
|
107
|
+
* .pipeTo(
|
|
108
|
+
* new WritableStream({
|
|
109
|
+
* write(record) {
|
|
110
|
+
* console.log(record);
|
|
111
|
+
* },
|
|
112
|
+
* }),
|
|
113
|
+
* );
|
|
114
|
+
* // Prints:
|
|
115
|
+
* // { name: 'Alice', age: '42' }
|
|
116
|
+
* // { name: 'Bob', age: '69' }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
function toStream<Header extends ReadonlyArray<string>>(csv: string, options?: ParseOptions<Header>): ReadableStream<CSVRecord<Header>>;
|
|
120
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import { CSVRecord, ParseOptions } from "./common/types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Parse CSV string stream to records.
|
|
4
|
+
*
|
|
5
|
+
* @category Middle-level API
|
|
6
|
+
* @param stream CSV string stream to parse
|
|
7
|
+
* @param options Parsing options.
|
|
8
|
+
* @returns Async iterable iterator of records.
|
|
9
|
+
*
|
|
10
|
+
* If you want array of records, use {@link parseStringStream.toArray} function.
|
|
11
|
+
*
|
|
12
|
+
* @example Parsing CSV files from strings
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { parseStringStream } from 'web-csv-toolbox';
|
|
16
|
+
*
|
|
17
|
+
* const csv = `name,age
|
|
18
|
+
* Alice,42
|
|
19
|
+
* Bob,69`;
|
|
20
|
+
*
|
|
21
|
+
* const stream = new ReadableStream({
|
|
22
|
+
* start(controller) {
|
|
23
|
+
* controller.enqueue(csv);
|
|
24
|
+
* controller.close();
|
|
25
|
+
* },
|
|
26
|
+
* });
|
|
27
|
+
*
|
|
28
|
+
* for await (const record of parseStringStream(csv)) {
|
|
29
|
+
* console.log(record);
|
|
30
|
+
* }
|
|
31
|
+
* // Prints:
|
|
32
|
+
* // { name: 'Alice', age: '42' }
|
|
33
|
+
* // { name: 'Bob', age: '69' }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function parseStringStream<Header extends ReadonlyArray<string>>(stream: ReadableStream<string>, options?: ParseOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
|
|
37
|
+
export declare namespace parseStringStream {
|
|
38
|
+
/**
|
|
39
|
+
* Parse CSV string stream to records.
|
|
40
|
+
*
|
|
41
|
+
* @returns Array of records
|
|
42
|
+
*
|
|
43
|
+
* @example
|
|
44
|
+
*
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { parseStringStream } from 'web-csv-toolbox';
|
|
47
|
+
*
|
|
48
|
+
* const csv = `name,age
|
|
49
|
+
* Alice,42
|
|
50
|
+
* Bob,69`;
|
|
51
|
+
*
|
|
52
|
+
* const stream = new ReadableStream({
|
|
53
|
+
* start(controller) {
|
|
54
|
+
* controller.enqueue(csv);
|
|
55
|
+
* controller.close();
|
|
56
|
+
* },
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* const records = await parseStringStream.toArray(stream);
|
|
60
|
+
* console.log(records);
|
|
61
|
+
* // Prints:
|
|
62
|
+
* // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
function toArray<Header extends ReadonlyArray<string>>(stream: ReadableStream<string>, options?: ParseOptions<Header>): Promise<CSVRecord<Header>[]>;
|
|
66
|
+
/**
|
|
67
|
+
* Parse CSV string stream to records.
|
|
68
|
+
*
|
|
69
|
+
* @returns Array of records
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
*
|
|
73
|
+
* ```ts
|
|
74
|
+
* import { parseStringStream } from 'web-csv-toolbox';
|
|
75
|
+
*
|
|
76
|
+
* const csv = `name,age
|
|
77
|
+
* Alice,42
|
|
78
|
+
* Bob,69`;
|
|
79
|
+
*
|
|
80
|
+
* const stream = new ReadableStream({
|
|
81
|
+
* start(controller) {
|
|
82
|
+
* controller.enqueue(csv);
|
|
83
|
+
* controller.close();
|
|
84
|
+
* },
|
|
85
|
+
* });
|
|
86
|
+
*
|
|
87
|
+
* await parseStringStream.toStream(stream)
|
|
88
|
+
* .pipeTo(
|
|
89
|
+
* new WritableStream({
|
|
90
|
+
* write(record) {
|
|
91
|
+
* console.log(record);
|
|
92
|
+
* },
|
|
93
|
+
* }),
|
|
94
|
+
* );
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
function toStream<Header extends ReadonlyArray<string>>(stream: ReadableStream<string>, options?: ParseOptions<Header>): ReadableStream<CSVRecord<Header>>;
|
|
98
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { CSVRecord, ParseBinaryOptions } from "./common/types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Parse CSV to records.
|
|
4
|
+
* This function is for parsing a binary stream.
|
|
5
|
+
*
|
|
6
|
+
* @category Middle-level API
|
|
7
|
+
* @remarks
|
|
8
|
+
* If you want to parse a string, use {@link parseStringStream}.
|
|
9
|
+
* @param stream CSV string to parse
|
|
10
|
+
* @param options Parsing options.
|
|
11
|
+
* @returns Async iterable iterator of records.
|
|
12
|
+
*
|
|
13
|
+
* If you want array of records, use {@link parseUint8ArrayStream.toArray} function.
|
|
14
|
+
*
|
|
15
|
+
* @example Parsing CSV binary
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* import { parseUint8ArrayStream } from 'web-csv-toolbox';
|
|
19
|
+
*
|
|
20
|
+
* const csv = Uint8Array.from([
|
|
21
|
+
* // ...
|
|
22
|
+
* ]);
|
|
23
|
+
*
|
|
24
|
+
* const stream = new ReadableStream({
|
|
25
|
+
* start(controller) {
|
|
26
|
+
* controller.enqueue(csv);
|
|
27
|
+
* controller.close();
|
|
28
|
+
* },
|
|
29
|
+
* });
|
|
30
|
+
*
|
|
31
|
+
* for await (const record of parseUint8ArrayStream(csv)) {
|
|
32
|
+
* console.log(record);
|
|
33
|
+
* }
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export declare function parseUint8ArrayStream<Header extends ReadonlyArray<string>>(stream: ReadableStream<Uint8Array>, options?: ParseBinaryOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
|
|
37
|
+
export declare namespace parseUint8ArrayStream {
|
|
38
|
+
/**
|
|
39
|
+
* Parse CSV binary to array of records,
|
|
40
|
+
* ideal for smaller data sets.
|
|
41
|
+
*
|
|
42
|
+
* @returns Array of records
|
|
43
|
+
*
|
|
44
|
+
* @example Parsing CSV binary
|
|
45
|
+
* ```ts
|
|
46
|
+
* import { parseUint8ArrayStream } from 'web-csv-toolbox';
|
|
47
|
+
*
|
|
48
|
+
* const csv = Uint8Array.from([
|
|
49
|
+
* // ...
|
|
50
|
+
* ]);
|
|
51
|
+
*
|
|
52
|
+
* const stream = new ReadableStream({
|
|
53
|
+
* start(controller) {
|
|
54
|
+
* controller.enqueue(csv);
|
|
55
|
+
* controller.close();
|
|
56
|
+
* },
|
|
57
|
+
* });
|
|
58
|
+
*
|
|
59
|
+
* const records = await parseUint8ArrayStream.toArray(stream);
|
|
60
|
+
* console.log(records);
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
function toArray<Header extends ReadonlyArray<string>>(stream: ReadableStream<Uint8Array>, options?: ParseBinaryOptions<Header>): Promise<CSVRecord<Header>[]>;
|
|
64
|
+
/**
|
|
65
|
+
* Parse CSV binary to array of records.
|
|
66
|
+
*
|
|
67
|
+
* @returns Stream of records
|
|
68
|
+
*
|
|
69
|
+
* @example Parsing CSV binary
|
|
70
|
+
* ```ts
|
|
71
|
+
* import { parseUint8ArrayStream } from 'web-csv-toolbox';
|
|
72
|
+
*
|
|
73
|
+
* const csv = Uint8Array.from([
|
|
74
|
+
* // ...
|
|
75
|
+
* ]);
|
|
76
|
+
*
|
|
77
|
+
* const stream = new ReadableStream({
|
|
78
|
+
* start(controller) {
|
|
79
|
+
* controller.enqueue(csv);
|
|
80
|
+
* controller.close();
|
|
81
|
+
* },
|
|
82
|
+
* });
|
|
83
|
+
*
|
|
84
|
+
* await parseUint8ArrayStream.toStream(stream)
|
|
85
|
+
* .pipeTo(new WritableStream({
|
|
86
|
+
* write(record) {
|
|
87
|
+
* console.log(record);
|
|
88
|
+
* },
|
|
89
|
+
* }),
|
|
90
|
+
* );
|
|
91
|
+
* ```
|
|
92
|
+
*/
|
|
93
|
+
function toStream<Header extends ReadonlyArray<string>>(stream: ReadableStream<Uint8Array>, options?: ParseBinaryOptions<Header>): ReadableStream<CSVRecord<Header>[]>;
|
|
94
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function convertIterableIteratorToAsync<T>(iterator: IterableIterator<T>): AsyncIterableIterator<T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function convertStreamToAsyncIterableIterator<T>(stream: ReadableStream<T>): AsyncIterableIterator<T>;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function convertThisAsyncIterableIteratorToArray<O, T extends (...args: any[]) => AsyncGenerator<O>>(this: T, ...args: Parameters<T>): Promise<O[]>;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escape a string for use in a regular expression.
|
|
3
|
+
*
|
|
4
|
+
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_expressions#escaping Regular expressions#Escaping | MDN}
|
|
5
|
+
* @param v string to escape
|
|
6
|
+
* @returns escaped string
|
|
7
|
+
*/
|
|
8
|
+
export declare function escapeRegExp(v: string): string;
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare function pipeline<I, T, O>(stream: ReadableStream<I>, transformer1: TransformStream<I, T>, transformer2: TransformStream<T, O>): ReadableStream<O>;
|
|
2
|
+
export declare function pipeline<I, T1, T2, O>(stream: ReadableStream<I>, transformer1: TransformStream<I, T1>, transformer2: TransformStream<T1, T2>, transformer3: TransformStream<T2, O>): ReadableStream<O>;
|
|
3
|
+
export declare function pipeline<I, T1, T2, T3, O>(stream: ReadableStream<I>, transformer1: TransformStream<I, T1>, transformer2: TransformStream<T1, T2>, transformer3: TransformStream<T2, T3>, transformer4: TransformStream<T3, O>): ReadableStream<O>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./common/constants.ts";
|
|
2
|
+
export * from "./common/types.ts";
|
|
3
|
+
export * from "./LexerTransformer.ts";
|
|
4
|
+
export * from "./RecordAssemblerTransformer.ts";
|
|
5
|
+
export * from "./parseString.ts";
|
|
6
|
+
export * from "./parseBinary.ts";
|
|
7
|
+
export * from "./parseUint8ArrayStream.ts";
|
|
8
|
+
export * from "./parseStringStream.ts";
|
|
9
|
+
export * from "./parseResponse.ts";
|
|
10
|
+
export * from "./parse.ts";
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self)
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).CSV={})}(this,(function(e){"use strict";const t=Symbol.for("web-csv-toolbox.FieldDelimiter"),r=Symbol.for("web-csv-toolbox.RecordDelimiter"),i=Symbol.for("web-csv-toolbox.Field"),n="\r\n",s="\n";function o(e){return e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}class a{#e;#t;#r;#i;#n;#s="";#o=!1;constructor({delimiter:e=",",quotation:t='"'}={}){!function(e){if("string"==typeof e.quotation&&0===e.quotation.length)throw new Error("quotation must not be empty");if("string"==typeof e.delimiter&&0===e.delimiter.length)throw new Error("delimiter must not be empty");if(e.quotation.includes(s)||e.quotation.includes("\r"))throw new Error("quotation must not include CR or LF");if(e.delimiter.includes(s)||e.delimiter.includes("\r"))throw new Error("delimiter must not include CR or LF");if(e.delimiter.includes(e.quotation)||e.quotation.includes(e.delimiter))throw new Error("delimiter and quotation must not include each other as a substring")}({delimiter:e,quotation:t}),this.#e=e,this.#t=e.length,this.#r=t,this.#i=t.length;const r=o(e),i=o(t);this.#n=new RegExp(`^(?:(?!${i})(?!${r})(?![\\r\\n]))([\\S\\s\\uFEFF\\xA0]+?)(?=${i}|${r}|\\r|\\n|$)`)}lex(e,t=!1){return t||(this.#o=!0),"string"==typeof e&&0!==e.length&&(this.#s+=e),this.#a()}flush(){return this.#o=!0,[...this.#a()]}*#a(){this.#o&&(this.#s.endsWith(n)?this.#s=this.#s.slice(0,-2):this.#s.endsWith(s)&&(this.#s=this.#s.slice(0,-1)));let e=null;for(let i;i=this.#u();)switch(i){case t:case r:e&&(yield e,e=null),yield i;break;default:e?e.value+=i.value:e=i}e&&(yield e)}#u(){if(0===this.#s.length)return null;if(!1===this.#o&&(this.#s===n||this.#s===s))return null;if(this.#s.startsWith(n))return this.#s=this.#s.slice(2),r;if(this.#s.startsWith(s))return this.#s=this.#s.slice(1),r;if(this.#s.startsWith(this.#e))return this.#s=this.#s.slice(this.#t),t;if(this.#s.startsWith(this.#r))return!1===this.#o&&this.#s.endsWith(this.#r)?null:this.#l();const e=this.#n.exec(this.#s);return e?!1===this.#o&&e[0].length===this.#s.length?null:(this.#s=this.#s.slice(e[0].length),{type:i,value:e[0]}):null}#l(){let e=this.#i,t="";for(;e<this.#s.length;)if(this.#s.slice(e,e+this.#i)!==this.#r||this.#s.slice(e+this.#i,e+2*this.#i)!==this.#r){if(this.#s.slice(e,e+this.#i)===this.#r)return!1===this.#o&&e+this.#i<this.#s.length&&this.#s.slice(e+this.#i,this.#t)!==this.#e&&this.#s.slice(e+this.#i,e+this.#i+2)!==n&&this.#s.slice(e+this.#i,e+this.#i+1)!==s?null:(this.#s=this.#s.slice(e+this.#i),{type:i,value:t});t+=this.#s[e],e++}else t+=this.#r,e+=2*this.#i;return null}}class u extends TransformStream{constructor(e={}){const t=new a(e);super({transform:(e,r)=>{0!==e.length&&r.enqueue([...t.lex(e,!0)])},flush:e=>{e.enqueue(t.flush())}})}}class l{#f=0;#h=[];#c;#d=!1;constructor(e={}){void 0!==e.header&&Array.isArray(e.header)&&this.#b(e.header)}*assemble(e,i=!0){for(const n of e)switch(n){case t:this.#f++,this.#d=!0;break;case r:void 0===this.#c?this.#b(this.#h):this.#d?yield Object.fromEntries(this.#c.map(((e,t)=>[e,this.#h.at(t)]))):yield Object.fromEntries(this.#c.map((e=>[e,""]))),this.#f=0,this.#h=new Array(this.#c?.length).fill(""),this.#d=!1;break;default:this.#d=!0,this.#h[this.#f]=n.value}i&&(yield*this.flush())}*flush(){void 0!==this.#c&&this.#d&&(yield Object.fromEntries(this.#c.filter((e=>e)).map(((e,t)=>[e,this.#h.at(t)]))))}#b(e){if(this.#c=e,0===this.#c.length)throw new Error("The header must not be empty.");if(new Set(this.#c).size!==this.#c.length)throw new Error("The header must not contain duplicate fields.")}}class f extends TransformStream{constructor(e={}){const t=new l(e);super({transform:(e,r)=>{for(const i of t.assemble(e,!1))r.enqueue(i)},flush:e=>{for(const r of t.flush())e.enqueue(r)}})}}function h(e,t){const r=new a(t),i=new l(t),n=r.lex(e);return[...i.assemble(n)]}function c(e,t){const r=new a(t),i=new l(t),n=r.lex(e);return i.assemble(n)}function d(e,t){const r=new a(t),i=new l(t);return new ReadableStream({start(t){const n=r.lex(e);for(const e of i.assemble(n))t.enqueue(e);t.close()}})}async function b(...e){const t=[];for await(const r of this(...e))t.push(r);return t}async function*m(e,t){yield*c(e,t)}function y(e,t){return new TextDecoder(t?.charset,{ignoreBOM:t?.ignoreBOM,fatal:t?.fatal}).decode(e instanceof ArrayBuffer?new Uint8Array(e):e)}function w(e,t={}){return c(y(e,t),t)}function g(e,t){return function(e){return{async next(){const t=e.next();return Promise.resolve(t)},[Symbol.asyncIterator](){return this}}}(w(e,t))}function p(e,...t){return new ReadableStream({start:r=>{t.reduce(((e,t)=>e.pipeThrough(t)),e).pipeTo(new WritableStream({write:e=>r.enqueue(e),close:()=>r.close()}))}})}function q(e,t){const{charset:r,fatal:i,ignoreBOM:n,decomposition:s}=t??{};return s?p(e,new DecompressionStream(s),new TextDecoderStream(r,{fatal:i,ignoreBOM:n}),new u(t),new f(t)):p(e,new TextDecoderStream(r,{fatal:i,ignoreBOM:n}),new u(t),new f(t))}async function*v(e){const t=e.getReader();for(;;){const{done:e,value:r}=await t.read();if(e)break;yield r}}function x(e,t){return v(q(e,t))}function S(e,t){return p(e,new u(t),new f(t))}function L(e,t){return v(S(e,t))}function A(e,t={}){const{headers:r}=e,i=r.get("content-type")??"text/csv",n=function(e){const[t,...r]=e.split(";"),i={type:t.trim(),parameters:{}};for(const n of r){const[e,t]=n.split("=");i.parameters[e.trim()]=t.trim()}return i}(i);if("text/csv"!==n.type)throw new Error(`Invalid mime type: ${i}`);return{decomposition:r.get("content-encoding")??void 0,charset:n.parameters.charset??"utf-8",...t}}function T(e,t){const r=A(e,t);if(null===e.body)throw new Error("Response body is null");return x(e.body,r)}async function*E(e,t){if("string"==typeof e)yield*m(e,t);else if(e instanceof Uint8Array||e instanceof ArrayBuffer)yield*g(e,t);else if(e instanceof ReadableStream){const[r,i]=e.tee(),n=r.getReader(),{value:s}=await n.read();n.releaseLock(),"string"==typeof s?yield*L(i,t):s instanceof Uint8Array&&(yield*x(i,t))}else e instanceof Response&&(yield*T(e,t))}Object.defineProperties(m,{toArray:{enumerable:!0,writable:!1,value:b},toArraySync:{enumerable:!0,writable:!1,value:h},toIterableIterator:{enumerable:!0,writable:!1,value:c},toStream:{enumerable:!0,writable:!1,value:d}}),Object.defineProperties(g,{toArray:{enumerable:!0,writable:!1,value:b},toArraySync:{enumerable:!0,writable:!1,value:function(e,t={}){return h(y(e,t),t)}},toIterableIterator:{enumerable:!0,writable:!1,value:w},toStream:{enumerable:!0,writable:!1,value:function(e,t={}){return d(y(e,t),t)}}}),Object.defineProperties(x,{toArray:{enumerable:!0,writable:!1,value:b},toStream:{enumerable:!0,writable:!1,value:q}}),Object.defineProperties(L,{toArray:{enumerable:!0,writable:!1,value:b},toStream:{enumerable:!0,writable:!1,value:S}}),Object.defineProperties(T,{toArray:{enumerable:!0,writable:!1,value:b},toStreamSync:{enumerable:!0,writable:!1,value:function(e,t){const r=A(e,t);if(null===e.body)throw new Error("Response body is null");return q(e.body,r)}}}),Object.defineProperties(E,{toArray:{enumerable:!0,writable:!1,value:b}}),e.Field=i,e.FieldDelimiter=t,e.LexerTransformer=u,e.RecordAssemblerTransformer=f,e.RecordDelimiter=r,e.parse=E,e.parseBinary=g,e.parseResponse=T,e.parseString=m,e.parseStringStream=L,e.parseUint8ArrayStream=x,Object.defineProperty(e,Symbol.toStringTag,{value:"Module"})}));
|
|
2
2
|
//# sourceMappingURL=web-csv-toolbox.umd.cjs.map
|