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,163 @@
|
|
|
1
|
+
import { Field, FieldDelimiter, RecordDelimiter } from "./constants.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Field token type.
|
|
4
|
+
* @category Types
|
|
5
|
+
*/
|
|
6
|
+
export interface FieldToken {
|
|
7
|
+
type: typeof Field;
|
|
8
|
+
value: string;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Token is a atomic unit of a CSV file.
|
|
12
|
+
* It can be a field, field delimiter, or record delimiter.
|
|
13
|
+
* @category Types
|
|
14
|
+
*/
|
|
15
|
+
export type Token = FieldToken | typeof FieldDelimiter | typeof RecordDelimiter;
|
|
16
|
+
/**
|
|
17
|
+
* CSV Common Options.
|
|
18
|
+
* @category Types
|
|
19
|
+
*/
|
|
20
|
+
export interface CommonOptions {
|
|
21
|
+
/**
|
|
22
|
+
* CSV field delimiter.
|
|
23
|
+
*
|
|
24
|
+
* @remarks
|
|
25
|
+
* If you want to parse TSV, specify `'\t'`.
|
|
26
|
+
*
|
|
27
|
+
* This library supports multi-character delimiters.
|
|
28
|
+
* @default ','
|
|
29
|
+
*/
|
|
30
|
+
delimiter?: string;
|
|
31
|
+
/**
|
|
32
|
+
* CSV field quotation.
|
|
33
|
+
*
|
|
34
|
+
* @remarks
|
|
35
|
+
* This library supports multi-character quotations.
|
|
36
|
+
*
|
|
37
|
+
* @default '"'
|
|
38
|
+
*/
|
|
39
|
+
quotation?: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* CSV Parsing Options for binary.
|
|
43
|
+
* @category Types
|
|
44
|
+
*/
|
|
45
|
+
export interface BinaryOptions {
|
|
46
|
+
/**
|
|
47
|
+
* If the binary is compressed by a compression algorithm,
|
|
48
|
+
* the decompressed CSV can be parsed by specifying the algorithm.
|
|
49
|
+
*
|
|
50
|
+
* @remarks
|
|
51
|
+
* Make sure the runtime you are running supports stream decompression.
|
|
52
|
+
*
|
|
53
|
+
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/DecompressionStream#browser_compatibility | DecompressionStream Compatibility}.
|
|
54
|
+
*/
|
|
55
|
+
decomposition?: CompressionFormat;
|
|
56
|
+
/**
|
|
57
|
+
* You can specify the character encoding of the binary.
|
|
58
|
+
*
|
|
59
|
+
* @remarks
|
|
60
|
+
* {@link !TextDecoderStream} is used internally.
|
|
61
|
+
*
|
|
62
|
+
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings | Encoding API Compatibility}
|
|
63
|
+
* for the encoding formats that can be specified.
|
|
64
|
+
*
|
|
65
|
+
* @default 'utf-8'
|
|
66
|
+
*/
|
|
67
|
+
charset?: string;
|
|
68
|
+
/**
|
|
69
|
+
* If the binary has a BOM, you can specify whether to ignore it.
|
|
70
|
+
*
|
|
71
|
+
* @remarks
|
|
72
|
+
* If you specify true, the BOM will be ignored.
|
|
73
|
+
* If you specify false or not specify it, the BOM will be treated as a normal character.
|
|
74
|
+
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream/ignoreBOM | TextDecoderOptions.ignoreBOM} for more information about the BOM.
|
|
75
|
+
* @default false
|
|
76
|
+
*/
|
|
77
|
+
ignoreBOM?: boolean;
|
|
78
|
+
/**
|
|
79
|
+
* If the binary has a invalid character, you can specify whether to throw an error.
|
|
80
|
+
*
|
|
81
|
+
* @remarks
|
|
82
|
+
* If the property is `true` then a decoder will throw a {@link !TypeError}
|
|
83
|
+
* if it encounters malformed data while decoding.
|
|
84
|
+
*
|
|
85
|
+
* If `false` the decoder will substitute the invalid data
|
|
86
|
+
* with the replacement character `U+FFFD` (�).
|
|
87
|
+
*
|
|
88
|
+
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream/fatal | TextDecoderOptions.fatal} for more information.
|
|
89
|
+
*
|
|
90
|
+
* @default false
|
|
91
|
+
*/
|
|
92
|
+
fatal?: boolean;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Record Assembler Options for CSV.
|
|
96
|
+
* @category Types
|
|
97
|
+
*
|
|
98
|
+
* @remarks
|
|
99
|
+
* If you specify `header: ['foo', 'bar']`,
|
|
100
|
+
* the first record will be treated as a normal record.
|
|
101
|
+
*
|
|
102
|
+
* If you don't specify `header`,
|
|
103
|
+
* the first record will be treated as a header.
|
|
104
|
+
*/
|
|
105
|
+
export interface RecordAssemblerOptions<Header extends ReadonlyArray<string>> {
|
|
106
|
+
/**
|
|
107
|
+
* CSV header.
|
|
108
|
+
*
|
|
109
|
+
* @remarks
|
|
110
|
+
* If you specify this option,
|
|
111
|
+
* the first record will be treated as a normal record.
|
|
112
|
+
*
|
|
113
|
+
* If you don't specify this option,
|
|
114
|
+
* the first record will be treated as a header.
|
|
115
|
+
*
|
|
116
|
+
* @default undefined
|
|
117
|
+
*/
|
|
118
|
+
header?: Header;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Parse options for CSV string.
|
|
122
|
+
* @category Types
|
|
123
|
+
*/
|
|
124
|
+
export interface ParseOptions<Header extends ReadonlyArray<string>> extends CommonOptions, RecordAssemblerOptions<Header> {
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Parse options for CSV binary.
|
|
128
|
+
* @category Types
|
|
129
|
+
*/
|
|
130
|
+
export interface ParseBinaryOptions<Header extends ReadonlyArray<string>> extends ParseOptions<Header>, BinaryOptions {
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* CSV Record.
|
|
134
|
+
* @category Types
|
|
135
|
+
* @template Header Header of the CSV.
|
|
136
|
+
*
|
|
137
|
+
* @example Header is ["foo", "bar"]
|
|
138
|
+
* ```ts
|
|
139
|
+
* const record: CSVRecord<["foo", "bar"]> = {
|
|
140
|
+
* foo: "1",
|
|
141
|
+
* bar: "2",
|
|
142
|
+
* };
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
export type CSVRecord<Header extends ReadonlyArray<string>> = Record<Header[number], string>;
|
|
146
|
+
/**
|
|
147
|
+
* CSV String.
|
|
148
|
+
*
|
|
149
|
+
* @category Types
|
|
150
|
+
*/
|
|
151
|
+
export type CSVString = string | ReadableStream<string>;
|
|
152
|
+
/**
|
|
153
|
+
* CSV Binary.
|
|
154
|
+
*
|
|
155
|
+
* @category Types
|
|
156
|
+
*/
|
|
157
|
+
export type CSVBinary = ReadableStream<Uint8Array> | Response | ArrayBuffer | Uint8Array;
|
|
158
|
+
/**
|
|
159
|
+
* CSV.
|
|
160
|
+
*
|
|
161
|
+
* @category Types
|
|
162
|
+
*/
|
|
163
|
+
export type CSV = CSVString | CSVBinary;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const CR = "\r";
|
|
2
|
+
export declare const CRLF = "\r\n";
|
|
3
|
+
export declare const LF = "\n";
|
|
4
|
+
/**
|
|
5
|
+
* COMMA is a symbol for comma(,).
|
|
6
|
+
*/
|
|
7
|
+
export declare const COMMA = ",";
|
|
8
|
+
/**
|
|
9
|
+
* DOUBLE_QUOTE is a symbol for double quote(").
|
|
10
|
+
*/
|
|
11
|
+
export declare const DOUBLE_QUOTE = "\"";
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CommonOptions } from "./common/types.ts";
|
|
2
|
+
export interface EscapeFieldOptions extends CommonOptions {
|
|
3
|
+
quote?: true;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* Escape the field.
|
|
7
|
+
*
|
|
8
|
+
* DO NOT USE THIS FUNCTION BEFORE ASSTPTED BY `{@link assertCommonOptions}`.
|
|
9
|
+
* @param value The field value to escape.
|
|
10
|
+
* @param options The options.
|
|
11
|
+
* @returns The escaped field.
|
|
12
|
+
*/
|
|
13
|
+
export declare function escapeField(value: string, { quotation, delimiter, quote, }?: EscapeFieldOptions): string;
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { CSVBinary, CSVRecord, CSVString, ParseBinaryOptions, ParseOptions } from "./common/types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Parse CSV to records.
|
|
4
|
+
*
|
|
5
|
+
* {@link !String}, {@link !ReadableStream}<string | {@link !Uint8Array}> and {@link !Response} are supported.
|
|
6
|
+
*
|
|
7
|
+
*
|
|
8
|
+
* @typeParam Header Header type like `['name', 'age']`.
|
|
9
|
+
*
|
|
10
|
+
* @param csv CSV string to parse.
|
|
11
|
+
* @param options Parsing options for CSV string parsing.
|
|
12
|
+
* @returns Async iterable iterator of records.
|
|
13
|
+
*
|
|
14
|
+
* If you want array of records, use {@link parse.toArray} function.
|
|
15
|
+
* @category High-level API
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* {@link parseString}, {@link parseBinary}, {@link parseUint8ArrayStream},
|
|
19
|
+
* {@link parseStringStream} and {@link parseResponse} are used internally.
|
|
20
|
+
*
|
|
21
|
+
* If you known the type of the CSV, it performs better to use them directly.
|
|
22
|
+
*
|
|
23
|
+
* | If you want to parse a... | Use... | Options... |
|
|
24
|
+
* | -------------------------------------------- | ----------------------------- | -------------------------- |
|
|
25
|
+
* | {@link !String} | {@link parseString} | {@link ParseOptions} |
|
|
26
|
+
* | {@link !ReadableStream}<{@link !String}> | {@link parseStringStream} | {@link ParseOptions} |
|
|
27
|
+
* | {@link !Uint8Array} \| {@link !ArrayBuffer} | {@link parseBinary} | {@link ParseBinaryOptions} |
|
|
28
|
+
* | {@link !ReadableStream}<{@link !Uint8Array}> | {@link parseUint8ArrayStream} | {@link ParseBinaryOptions} |
|
|
29
|
+
* | {@link !Response} | {@link parseResponse} | {@link ParseBinaryOptions} |
|
|
30
|
+
*
|
|
31
|
+
* @example Parsing CSV files from strings
|
|
32
|
+
*
|
|
33
|
+
* ```ts
|
|
34
|
+
* import { parse } from 'web-csv-toolbox';
|
|
35
|
+
*
|
|
36
|
+
* const csv = `name,age
|
|
37
|
+
* Alice,42
|
|
38
|
+
* Bob,69`;
|
|
39
|
+
*
|
|
40
|
+
* for await (const record of parse(csv)) {
|
|
41
|
+
* console.log(record);
|
|
42
|
+
* }
|
|
43
|
+
* // Prints:
|
|
44
|
+
* // { name: 'Alice', age: '42' }
|
|
45
|
+
* // { name: 'Bob', age: '69' }
|
|
46
|
+
* ```
|
|
47
|
+
*
|
|
48
|
+
* @example Parsing CSV files from streams
|
|
49
|
+
*
|
|
50
|
+
* ```ts
|
|
51
|
+
* import { parse } from 'web-csv-toolbox';
|
|
52
|
+
*
|
|
53
|
+
* const csv = `name,age
|
|
54
|
+
* Alice,42
|
|
55
|
+
* Bob,69`;
|
|
56
|
+
*
|
|
57
|
+
* const stream = new ReadableStream({
|
|
58
|
+
* start(controller) {
|
|
59
|
+
* controller.enqueue(csv);
|
|
60
|
+
* controller.close();
|
|
61
|
+
* }
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* for await (const record of parse(stream)) {
|
|
65
|
+
* console.log(record);
|
|
66
|
+
* }
|
|
67
|
+
* // Prints:
|
|
68
|
+
* // { name: 'Alice', age: '42' }
|
|
69
|
+
* // { name: 'Bob', age: '69' }
|
|
70
|
+
* ```
|
|
71
|
+
*
|
|
72
|
+
*
|
|
73
|
+
* @example Parsing CSV files with headers
|
|
74
|
+
*
|
|
75
|
+
* ```ts
|
|
76
|
+
* import { parse } from 'web-csv-toolbox';
|
|
77
|
+
*
|
|
78
|
+
* // This CSV has no header.
|
|
79
|
+
* const csv = `Alice,42
|
|
80
|
+
* Bob,69`;
|
|
81
|
+
*
|
|
82
|
+
* for await (const record of parse(csv, { header: ['name', 'age'] })) {
|
|
83
|
+
* console.log(record);
|
|
84
|
+
* }
|
|
85
|
+
* // Prints:
|
|
86
|
+
* // { name: 'Alice', age: '42' }
|
|
87
|
+
* // { name: 'Bob', age: '69' }
|
|
88
|
+
* ```
|
|
89
|
+
*
|
|
90
|
+
* @example Parsing CSV files with different delimiters characters
|
|
91
|
+
*
|
|
92
|
+
* ```ts
|
|
93
|
+
* import { parse } from 'web-csv-toolbox';
|
|
94
|
+
*
|
|
95
|
+
* const csv = `name\tage
|
|
96
|
+
* Alice\t42
|
|
97
|
+
* Bob\t69`;
|
|
98
|
+
*
|
|
99
|
+
* for await (const record of parse(csv, { delimiter: '\t' })) {
|
|
100
|
+
* console.log(record);
|
|
101
|
+
* }
|
|
102
|
+
* // Prints:
|
|
103
|
+
* // { name: 'Alice', age: '42' }
|
|
104
|
+
* // { name: 'Bob', age: '69' }
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
export declare function parse<Header extends ReadonlyArray<string>>(csv: CSVString, options?: ParseOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
|
|
108
|
+
/**
|
|
109
|
+
* Parse CSV binary to records.
|
|
110
|
+
*
|
|
111
|
+
* @param csv CSV binary to parse.
|
|
112
|
+
* @param options Parsing options for CSV binary parsing.
|
|
113
|
+
*
|
|
114
|
+
* @example Parsing CSV files from responses
|
|
115
|
+
*
|
|
116
|
+
* ```ts
|
|
117
|
+
* import { parse } from 'web-csv-toolbox';
|
|
118
|
+
*
|
|
119
|
+
* // This CSV data is not gzipped and encoded in utf-8.
|
|
120
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
121
|
+
*
|
|
122
|
+
* for await (const record of parse(response)) {
|
|
123
|
+
* // ...
|
|
124
|
+
* }
|
|
125
|
+
* ```
|
|
126
|
+
*
|
|
127
|
+
* @example Parsing CSV files with options spcialized for binary
|
|
128
|
+
*
|
|
129
|
+
* ```ts
|
|
130
|
+
* import { parse } from 'web-csv-toolbox';
|
|
131
|
+
*
|
|
132
|
+
* // This CSV data is gzipped and encoded in shift-jis and has BOM.
|
|
133
|
+
* const response = await fetch('https://example.com/data.csv.gz');
|
|
134
|
+
*
|
|
135
|
+
* for await (const record of parse(response, {
|
|
136
|
+
* charset: 'shift-jis',
|
|
137
|
+
* ignoreBOM: true,
|
|
138
|
+
* decomposition: 'gzip',
|
|
139
|
+
* })) {
|
|
140
|
+
* // ...
|
|
141
|
+
* }
|
|
142
|
+
* ```
|
|
143
|
+
*/
|
|
144
|
+
export declare function parse<Header extends ReadonlyArray<string>>(csv: CSVBinary, options?: ParseBinaryOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
|
|
145
|
+
export declare namespace parse {
|
|
146
|
+
/**
|
|
147
|
+
* Parse CSV string to array of records,
|
|
148
|
+
* ideal for smaller data sets.
|
|
149
|
+
*
|
|
150
|
+
* @example Parse a CSV as array of records
|
|
151
|
+
*
|
|
152
|
+
* ```ts
|
|
153
|
+
* import { parse } from 'web-csv-toolbox';
|
|
154
|
+
*
|
|
155
|
+
* const csv = `name,age
|
|
156
|
+
* Alice,42
|
|
157
|
+
* Bob,69`;
|
|
158
|
+
*
|
|
159
|
+
* const records = await parse.toArray(csv);
|
|
160
|
+
* console.log(records);
|
|
161
|
+
* // Prints:
|
|
162
|
+
* // [ { name: 'Alice', age: '42' }, { name: 'Bob', age: '69' } ]
|
|
163
|
+
* ```
|
|
164
|
+
*/
|
|
165
|
+
function toArray<Header extends ReadonlyArray<string>>(csv: CSVString, options?: ParseOptions<Header>): Promise<CSVRecord<Header>[]>;
|
|
166
|
+
/**
|
|
167
|
+
* Parse CSV string to array of records,
|
|
168
|
+
* ideal for smaller data sets.
|
|
169
|
+
*
|
|
170
|
+
* @example Parse a CSV as array of records
|
|
171
|
+
*
|
|
172
|
+
* ```ts
|
|
173
|
+
* import { parse } from 'web-csv-toolbox';
|
|
174
|
+
*
|
|
175
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
176
|
+
*
|
|
177
|
+
* const records = await parse.toArray(response);
|
|
178
|
+
* console.log(records);
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
function toArray<Header extends ReadonlyArray<string>>(csv: CSVBinary, options?: ParseBinaryOptions<Header>): Promise<CSVRecord<Header>[]>;
|
|
182
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { CSVRecord, ParseBinaryOptions } from "./common/types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Parse a binary from an {@link !Uint8Array}.
|
|
4
|
+
*
|
|
5
|
+
* @category Middle-level API
|
|
6
|
+
*
|
|
7
|
+
* @param bytes CSV bytes to parse.
|
|
8
|
+
* @param options Parsing options
|
|
9
|
+
* @returns Async iterable iterator of records.
|
|
10
|
+
*
|
|
11
|
+
* @example Parsing CSV binary
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* import { parseUint8Array } from 'web-csv-toolbox';
|
|
15
|
+
*
|
|
16
|
+
* const csv = Uint8Array.from([
|
|
17
|
+
* // ...
|
|
18
|
+
* ]);
|
|
19
|
+
*
|
|
20
|
+
* for await (const record of parseUint8Array(csv)) {
|
|
21
|
+
* console.log(record);
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
export declare function parseBinary<Header extends ReadonlyArray<string>>(bytes: Uint8Array | ArrayBuffer, options?: ParseBinaryOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
|
|
26
|
+
export declare namespace parseBinary {
|
|
27
|
+
/**
|
|
28
|
+
* Parse a binary from an {@link !Uint8Array} to an array of records.
|
|
29
|
+
*
|
|
30
|
+
* @param bytes CSV bytes to parse.
|
|
31
|
+
* @param options Parsing options
|
|
32
|
+
* @returns Array of records
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```ts
|
|
36
|
+
* import { parseUint8Array } from 'web-csv-toolbox';
|
|
37
|
+
*
|
|
38
|
+
* const csv = Uint8Array.from([
|
|
39
|
+
* // ...
|
|
40
|
+
* ]);
|
|
41
|
+
*
|
|
42
|
+
* const records = await parseUint8Array.toArray(csv);
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
function toArray<Header extends ReadonlyArray<string>>(bytes: Uint8Array | ArrayBuffer, options?: ParseBinaryOptions<Header>): Promise<CSVRecord<Header>[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Parse a binary from an {@link !Uint8Array} to an array of records.
|
|
48
|
+
*
|
|
49
|
+
* @param bytes CSV bytes to parse.
|
|
50
|
+
* @param options Parsing options
|
|
51
|
+
* @returns Array of records
|
|
52
|
+
* @example
|
|
53
|
+
*
|
|
54
|
+
* ```ts
|
|
55
|
+
* import { parseUint8Array } from 'web-csv-toolbox';
|
|
56
|
+
*
|
|
57
|
+
* const csv = Uint8Array.from([
|
|
58
|
+
* // ...
|
|
59
|
+
* ]);
|
|
60
|
+
*
|
|
61
|
+
* const records = parseUint8Array.toArraySync(csv);
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
function toArraySync<Header extends ReadonlyArray<string>>(bytes: Uint8Array | ArrayBuffer, options?: ParseBinaryOptions<Header>): CSVRecord<Header>[];
|
|
65
|
+
/**
|
|
66
|
+
* Parse a binary from an {@link !Uint8Array} to an iterable iterator of records.
|
|
67
|
+
*
|
|
68
|
+
* @param bytes CSV bytes to parse.
|
|
69
|
+
* @param options Parsing options
|
|
70
|
+
* @returns Async iterable iterator of records.
|
|
71
|
+
* @example
|
|
72
|
+
* ```ts
|
|
73
|
+
* import { parseUint8Array } from 'web-csv-toolbox';
|
|
74
|
+
*
|
|
75
|
+
* const csv = Uint8Array.from([
|
|
76
|
+
* // ...
|
|
77
|
+
* ]);
|
|
78
|
+
*
|
|
79
|
+
* for (const record of parseUint8Array.toIterableIterator(csv)) {
|
|
80
|
+
* console.log(record);
|
|
81
|
+
* }
|
|
82
|
+
* ```
|
|
83
|
+
*/
|
|
84
|
+
function toIterableIterator<Header extends ReadonlyArray<string>>(bytes: Uint8Array, options?: ParseBinaryOptions<Header>): IterableIterator<CSVRecord<Header>>;
|
|
85
|
+
/**
|
|
86
|
+
* Parse a binary from an {@link !Uint8Array} to a stream of records.
|
|
87
|
+
*
|
|
88
|
+
* @param bytes CSV bytes to parse.
|
|
89
|
+
* @param options Parsing options
|
|
90
|
+
* @returns Stream of records.
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
*
|
|
94
|
+
* ```ts
|
|
95
|
+
* import { parseUint8Array } from 'web-csv-toolbox';
|
|
96
|
+
*
|
|
97
|
+
* const csv = Uint8Array.from([
|
|
98
|
+
* // ...
|
|
99
|
+
* ]);
|
|
100
|
+
*
|
|
101
|
+
* const stream = parseUint8Array.toStream(csv);
|
|
102
|
+
*
|
|
103
|
+
* await stream.pipeTo(
|
|
104
|
+
* new WritableStream({
|
|
105
|
+
* write(record) {
|
|
106
|
+
* console.log(record);
|
|
107
|
+
* },
|
|
108
|
+
* }),
|
|
109
|
+
* );
|
|
110
|
+
* ```
|
|
111
|
+
*/
|
|
112
|
+
function toStream<Header extends ReadonlyArray<string>>(bytes: Uint8Array, options?: ParseBinaryOptions<Header>): ReadableStream<CSVRecord<Header>>;
|
|
113
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { CSVRecord, ParseOptions } from "./common/types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Parse HTTP Response what contains CSV to records,
|
|
4
|
+
* ideal for smaller data sets.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* This function automatically treats response headers.
|
|
8
|
+
*
|
|
9
|
+
* - If `Content-Type` header is not set, it assumes `text/csv`.
|
|
10
|
+
* - If `Content-Type` header is not `text/csv`, it throws an error.
|
|
11
|
+
* - If `Content-Type` header has charset parameter, it uses it for decoding.
|
|
12
|
+
* - If `Content-Encoding` header is set, it decompresses the response.
|
|
13
|
+
* - Should there be any conflicting information between the header and the options, the option's value will take precedence.
|
|
14
|
+
*
|
|
15
|
+
* @category Middle-level API
|
|
16
|
+
* @param response
|
|
17
|
+
* @param options
|
|
18
|
+
* @returns Async iterable iterator of records.
|
|
19
|
+
*
|
|
20
|
+
* If you want array of records, use {@link parseResponse.toArray} function.
|
|
21
|
+
*
|
|
22
|
+
* @example Parsing CSV Response
|
|
23
|
+
*
|
|
24
|
+
* ```ts
|
|
25
|
+
* import { parseResponse } from 'web-csv-toolbox';
|
|
26
|
+
*
|
|
27
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
28
|
+
*
|
|
29
|
+
* for await (const record of parseResponse(response)) {
|
|
30
|
+
* console.log(record);
|
|
31
|
+
* }
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function parseResponse<Header extends ReadonlyArray<string>>(response: Response, options?: ParseOptions<Header>): AsyncIterableIterator<CSVRecord<Header>>;
|
|
35
|
+
export declare namespace parseResponse {
|
|
36
|
+
/**
|
|
37
|
+
* Parse CSV Response to array of records.
|
|
38
|
+
*
|
|
39
|
+
* @returns Array of records
|
|
40
|
+
*
|
|
41
|
+
* @example Parsing CSV Response
|
|
42
|
+
*
|
|
43
|
+
* ```ts
|
|
44
|
+
* import { parseResponse } from 'web-csv-toolbox';
|
|
45
|
+
*
|
|
46
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
47
|
+
*
|
|
48
|
+
* const records = await parseResponse.toArray(response);
|
|
49
|
+
* console.log(records);
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
function toArray<Header extends ReadonlyArray<string>>(response: Response, options?: ParseOptions<Header>): Promise<CSVRecord<Header>[]>;
|
|
53
|
+
/**
|
|
54
|
+
* Parse CSV Response to stream of records.
|
|
55
|
+
*
|
|
56
|
+
* @param response Response to parse
|
|
57
|
+
* @returns Stream of records
|
|
58
|
+
*
|
|
59
|
+
* @example Parsing CSV Response
|
|
60
|
+
*
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { parseResponse } from 'web-csv-toolbox';
|
|
63
|
+
*
|
|
64
|
+
* const response = await fetch('https://example.com/data.csv');
|
|
65
|
+
*
|
|
66
|
+
* await parseResponse.toStream(response)
|
|
67
|
+
* .pipeTo(
|
|
68
|
+
* new WritableStream({
|
|
69
|
+
* write(record) {
|
|
70
|
+
* console.log(record);
|
|
71
|
+
* },
|
|
72
|
+
* }),
|
|
73
|
+
* );
|
|
74
|
+
* // Prints:
|
|
75
|
+
* // { name: 'Alice', age: '42' }
|
|
76
|
+
* // { name: 'Bob', age: '69' }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
function toStream<Header extends ReadonlyArray<string>>(response: Response, options?: ParseOptions<Header>): ReadableStream<CSVRecord<Header>[]>;
|
|
80
|
+
}
|