-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
types.ts
183 lines (172 loc) · 4.3 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type { Field, FieldDelimiter, RecordDelimiter } from "./constants.ts";
/**
* Field token type.
* @category Types
*/
export interface FieldToken {
type: typeof Field;
value: string;
}
/**
* Token is a atomic unit of a CSV file.
* It can be a field, field delimiter, or record delimiter.
* @category Types
*/
export type Token = FieldToken | typeof FieldDelimiter | typeof RecordDelimiter;
/**
* CSV Common Options.
* @category Types
*/
export interface CommonOptions {
/**
* CSV field delimiter.
*
* @remarks
* If you want to parse TSV, specify `'\t'`.
*
* This library supports multi-character delimiters.
* @default ','
*/
delimiter?: string;
/**
* CSV field quotation.
*
* @remarks
* This library supports multi-character quotations.
*
* @default '"'
*/
quotation?: string;
}
/**
* CSV Parsing Options for binary.
* @category Types
*/
export interface BinaryOptions {
/**
* If the binary is compressed by a compression algorithm,
* the decompressed CSV can be parsed by specifying the algorithm.
*
* @remarks
* Make sure the runtime you are running supports stream decompression.
*
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/DecompressionStream#browser_compatibility | DecompressionStream Compatibility}.
*/
decomposition?: CompressionFormat;
/**
* You can specify the character encoding of the binary.
*
* @remarks
* {@link !TextDecoderStream} is used internally.
*
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/Encoding_API/Encodings | Encoding API Compatibility}
* for the encoding formats that can be specified.
*
* @default 'utf-8'
*/
charset?: string;
/**
* If the binary has a BOM, you can specify whether to ignore it.
*
* @remarks
* If you specify true, the BOM will be ignored.
* If you specify false or not specify it, the BOM will be treated as a normal character.
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream/ignoreBOM | TextDecoderOptions.ignoreBOM} for more information about the BOM.
* @default false
*/
ignoreBOM?: boolean;
/**
* If the binary has a invalid character, you can specify whether to throw an error.
*
* @remarks
* If the property is `true` then a decoder will throw a {@link !TypeError}
* if it encounters malformed data while decoding.
*
* If `false` the decoder will substitute the invalid data
* with the replacement character `U+FFFD` (�).
*
* See {@link https://developer.mozilla.org/en-US/docs/Web/API/TextDecoderStream/fatal | TextDecoderOptions.fatal} for more information.
*
* @default false
*/
fatal?: boolean;
}
/**
* Record Assembler Options for CSV.
* @category Types
*
* @remarks
* If you specify `header: ['foo', 'bar']`,
* the first record will be treated as a normal record.
*
* If you don't specify `header`,
* the first record will be treated as a header.
*/
export interface RecordAssemblerOptions<Header extends ReadonlyArray<string>> {
/**
* CSV header.
*
* @remarks
* If you specify this option,
* the first record will be treated as a normal record.
*
* If you don't specify this option,
* the first record will be treated as a header.
*
* @default undefined
*/
header?: Header;
}
/**
* Parse options for CSV string.
* @category Types
*/
export interface ParseOptions<Header extends ReadonlyArray<string>>
extends CommonOptions,
RecordAssemblerOptions<Header> {}
/**
* Parse options for CSV binary.
* @category Types
*/
export interface ParseBinaryOptions<Header extends ReadonlyArray<string>>
extends ParseOptions<Header>,
BinaryOptions {}
/**
* CSV Record.
* @category Types
* @template Header Header of the CSV.
*
* @example Header is ["foo", "bar"]
* ```ts
* const record: CSVRecord<["foo", "bar"]> = {
* foo: "1",
* bar: "2",
* };
* ```
*/
export type CSVRecord<Header extends ReadonlyArray<string>> = Record<
Header[number],
string
>;
/**
* CSV String.
*
* @category Types
*/
export type CSVString = string | ReadableStream<string>;
/**
* CSV Binary.
*
* @category Types
*/
export type CSVBinary =
| ReadableStream<Uint8Array>
| Response
| ArrayBuffer
| Uint8Array;
/**
* CSV.
*
* @category Types
*/
export type CSV = CSVString | CSVBinary;