Skip to content

Commit da7a62e

Browse files
committed
feat(csv-parse): normailzsed options type
1 parent 423db5a commit da7a62e

File tree

3 files changed

+211
-43
lines changed

3 files changed

+211
-43
lines changed

packages/csv-parse/lib/index.d.ts

Lines changed: 209 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,42 @@ export class Parser {
1919

2020
__write(chars: any, end: any, callback: any): any;
2121

22-
readonly options: Options;
22+
readonly options: OptionsNormalized;
2323

2424
readonly info: Info;
2525
}
2626

27+
export interface Info {
28+
/**
29+
* Count the number of lines being fully commented.
30+
*/
31+
readonly comment_lines: number;
32+
/**
33+
* Count the number of processed empty lines.
34+
*/
35+
readonly empty_lines: number;
36+
/**
37+
* The number of lines encountered in the source dataset, start at 1 for the first line.
38+
*/
39+
readonly lines: number;
40+
/**
41+
* Count the number of processed records.
42+
*/
43+
readonly records: number;
44+
/**
45+
* Count of the number of processed bytes.
46+
*/
47+
readonly bytes: number;
48+
/**
49+
* Number of non uniform records when `relax_column_count` is true.
50+
*/
51+
readonly invalid_field_length: number;
52+
/**
53+
* Normalized verion of `options.columns` when `options.columns` is true, boolean otherwise.
54+
*/
55+
readonly columns: boolean | { name: string }[] | { disabled: true }[];
56+
}
57+
2758
export interface CastingContext {
2859
readonly column: number | string;
2960
readonly empty_lines: number;
@@ -51,6 +82,166 @@ export type ColumnOption<K = string> =
5182
| false
5283
| { name: K };
5384

85+
export interface OptionsNormalized<T = string[]> {
86+
auto_parse?: boolean | CastingFunction;
87+
auto_parse_date?: boolean | CastingDateFunction;
88+
/**
89+
* If true, detect and exclude the byte order mark (BOM) from the CSV input if present.
90+
*/
91+
bom?: boolean;
92+
/**
93+
* If true, the parser will attempt to convert input string to native types.
94+
* If a function, receive the value as first argument, a context as second argument and return a new value. More information about the context properties is available below.
95+
*/
96+
cast?: boolean | CastingFunction;
97+
/**
98+
* If true, the parser will attempt to convert input string to dates.
99+
* If a function, receive the value as argument and return a new value. It requires the "auto_parse" option. Be careful, it relies on Date.parse.
100+
*/
101+
cast_date?: boolean | CastingDateFunction;
102+
/**
103+
* Internal property string the function to
104+
*/
105+
cast_first_line_to_header?: (
106+
record: T,
107+
) => ColumnOption<
108+
T extends string[] ? string : T extends unknown ? string : keyof T
109+
>[];
110+
/**
111+
* List of fields as an array, a user defined callback accepting the first
112+
* line and returning the column names or true if autodiscovered in the first
113+
* CSV line, default to null, affect the result data set in the sense that
114+
* records will be objects instead of arrays.
115+
*/
116+
columns:
117+
| boolean
118+
| ColumnOption<
119+
T extends string[] ? string : T extends unknown ? string : keyof T
120+
>[];
121+
/**
122+
* Convert values into an array of values when columns are activated and
123+
* when multiple columns of the same name are found.
124+
*/
125+
group_columns_by_name: boolean;
126+
/**
127+
* Treat all the characters after this one as a comment, default to '' (disabled).
128+
*/
129+
comment: string | null;
130+
/**
131+
* Restrict the definition of comments to a full line. Comment characters
132+
* defined in the middle of the line are not interpreted as such. The
133+
* option require the activation of comments.
134+
*/
135+
comment_no_infix: boolean;
136+
/**
137+
* Set the field delimiter. One character only, defaults to comma.
138+
*/
139+
delimiter: Buffer[];
140+
/**
141+
* Set the source and destination encoding, a value of `null` returns buffer instead of strings.
142+
*/
143+
encoding: BufferEncoding | null;
144+
/**
145+
* Set the escape character, one character only, defaults to double quotes.
146+
*/
147+
escape: null | Buffer;
148+
/**
149+
* Start handling records from the requested number of records.
150+
*/
151+
from: number;
152+
/**
153+
* Start handling records from the requested line number.
154+
*/
155+
from_line: number;
156+
/**
157+
* Don't interpret delimiters as such in the last field according to the number of fields calculated from the number of columns, the option require the presence of the `column` option when `true`.
158+
*/
159+
ignore_last_delimiters: boolean | number;
160+
/**
161+
* Generate two properties `info` and `record` where `info` is a snapshot of the info object at the time the record was created and `record` is the parsed array or object.
162+
*/
163+
info: boolean;
164+
/**
165+
* If true, ignore whitespace immediately following the delimiter (i.e. left-trim all fields), defaults to false.
166+
* Does not remove whitespace in a quoted field.
167+
*/
168+
ltrim: boolean;
169+
/**
170+
* Maximum numer of characters to be contained in the field and line buffers before an exception is raised,
171+
* used to guard against a wrong delimiter or record_delimiter,
172+
* default to 128000 characters.
173+
*/
174+
max_record_size: number;
175+
/**
176+
* Name of header-record title to name objects by.
177+
*/
178+
objname: number | string | undefined;
179+
/**
180+
* Alter and filter records by executing a user defined function.
181+
*/
182+
on_record?: (record: T, context: CastingContext) => T | undefined;
183+
/**
184+
* Optional character surrounding a field, one character only, defaults to double quotes.
185+
*/
186+
quote?: Buffer | null;
187+
/**
188+
* Generate two properties raw and row where raw is the original CSV row content and row is the parsed array or object.
189+
*/
190+
raw: boolean;
191+
/**
192+
* Discard inconsistent columns count, default to false.
193+
*/
194+
relax_column_count: boolean;
195+
/**
196+
* Discard inconsistent columns count when the record contains less fields than expected, default to false.
197+
*/
198+
relax_column_count_less: boolean;
199+
/**
200+
* Discard inconsistent columns count when the record contains more fields than expected, default to false.
201+
*/
202+
relax_column_count_more: boolean;
203+
/**
204+
* Preserve quotes inside unquoted field.
205+
*/
206+
relax_quotes: boolean;
207+
/**
208+
* One or multiple characters used to delimit record rows; defaults to auto discovery if not provided.
209+
* Supported auto discovery method are Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters.
210+
*/
211+
record_delimiter: Buffer[];
212+
/**
213+
* If true, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields), defaults to false.
214+
* Does not remove whitespace in a quoted field.
215+
*/
216+
rtrim: boolean;
217+
/**
218+
* Dont generate empty values for empty lines.
219+
* Defaults to false
220+
*/
221+
skip_empty_lines: boolean;
222+
/**
223+
* Skip a line with error found inside and directly go process the next line.
224+
*/
225+
skip_records_with_error: boolean;
226+
/**
227+
* Don't generate records for lines containing empty column values (column matching /\s*\/), defaults to false.
228+
*/
229+
skip_records_with_empty_values: boolean;
230+
/**
231+
* Stop handling records after the requested number of records.
232+
*/
233+
to: number;
234+
/**
235+
* Stop handling records after the requested line number.
236+
*/
237+
to_line: number;
238+
/**
239+
* If true, ignore whitespace immediately around the delimiter, defaults to false.
240+
* Does not remove whitespace in a quoted field.
241+
*/
242+
trim: boolean;
243+
}
244+
54245
/*
55246
Note, could not `extends stream.TransformOptions` because encoding can be
56247
BufferEncoding and undefined as well as null which is not defined in the
@@ -123,20 +314,20 @@ export interface Options<T = string[]> {
123314
/**
124315
* Set the source and destination encoding, a value of `null` returns buffer instead of strings.
125316
*/
126-
encoding?: BufferEncoding | undefined;
317+
encoding?: BufferEncoding | boolean | null | undefined;
127318
/**
128319
* Set the escape character, one character only, defaults to double quotes.
129320
*/
130-
escape?: string | null | false | Buffer;
321+
escape?: string | null | boolean | Buffer;
131322
/**
132323
* Start handling records from the requested number of records.
133324
*/
134-
from?: number;
325+
from?: number | string;
135326
/**
136327
* Start handling records from the requested line number.
137328
*/
138-
from_line?: number;
139-
fromLine?: number;
329+
from_line?: null | number | string;
330+
fromLine?: null | number | string;
140331
/**
141332
* Don't interpret delimiters as such in the last field according to the number of fields calculated from the number of columns, the option require the presence of the `column` option when `true`.
142333
*/
@@ -149,23 +340,29 @@ export interface Options<T = string[]> {
149340
* If true, ignore whitespace immediately following the delimiter (i.e. left-trim all fields), defaults to false.
150341
* Does not remove whitespace in a quoted field.
151342
*/
152-
ltrim?: boolean;
343+
ltrim?: boolean | null;
153344
/**
154345
* Maximum numer of characters to be contained in the field and line buffers before an exception is raised,
155346
* used to guard against a wrong delimiter or record_delimiter,
156347
* default to 128000 characters.
157348
*/
158-
max_record_size?: number;
349+
max_record_size?: number | null | string;
159350
maxRecordSize?: number;
160351
/**
161352
* Name of header-record title to name objects by.
162353
*/
163-
objname?: string;
354+
objname?: Buffer | null | number | string;
164355
/**
165356
* Alter and filter records by executing a user defined function.
166357
*/
167358
on_record?: (record: T, context: CastingContext) => T | null | undefined;
168359
onRecord?: (record: T, context: CastingContext) => T | null | undefined;
360+
/**
361+
* Function called when an error occured if the `skip_records_with_error`
362+
* option is activated.
363+
*/
364+
on_skip?: (err: CsvError | undefined, raw: string | undefined) => undefined;
365+
onSkip?: (err: CsvError | undefined, raw: string | undefined) => undefined;
169366
/**
170367
* Optional character surrounding a field, one character only, defaults to double quotes.
171368
*/
@@ -204,7 +401,7 @@ export interface Options<T = string[]> {
204401
* If true, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields), defaults to false.
205402
* Does not remove whitespace in a quoted field.
206403
*/
207-
rtrim?: boolean;
404+
rtrim?: boolean | null;
208405
/**
209406
* Dont generate empty values for empty lines.
210407
* Defaults to false
@@ -234,38 +431,7 @@ export interface Options<T = string[]> {
234431
* If true, ignore whitespace immediately around the delimiter, defaults to false.
235432
* Does not remove whitespace in a quoted field.
236433
*/
237-
trim?: boolean;
238-
}
239-
240-
export interface Info {
241-
/**
242-
* Count the number of lines being fully commented.
243-
*/
244-
readonly comment_lines: number;
245-
/**
246-
* Count the number of processed empty lines.
247-
*/
248-
readonly empty_lines: number;
249-
/**
250-
* The number of lines encountered in the source dataset, start at 1 for the first line.
251-
*/
252-
readonly lines: number;
253-
/**
254-
* Count the number of processed records.
255-
*/
256-
readonly records: number;
257-
/**
258-
* Count of the number of processed bytes.
259-
*/
260-
readonly bytes: number;
261-
/**
262-
* Number of non uniform records when `relax_column_count` is true.
263-
*/
264-
readonly invalid_field_length: number;
265-
/**
266-
* Normalized verion of `options.columns` when `options.columns` is true, boolean otherwise.
267-
*/
268-
readonly columns: boolean | { name: string }[] | { disabled: true }[];
434+
trim?: boolean | null;
269435
}
270436

271437
export type CsvErrorCode =
@@ -297,7 +463,7 @@ export class CsvError extends Error {
297463
constructor(
298464
code: CsvErrorCode,
299465
message: string | string[],
300-
options?: Options,
466+
options?: OptionsNormalized,
301467
...contexts: any[]
302468
);
303469
}

packages/csv-parse/lib/stream.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export {
1010
CastingDateFunction,
1111
ColumnOption,
1212
Options,
13+
OptionsNormalized,
1314
Info,
1415
CsvErrorCode,
1516
CsvError,

packages/csv-parse/lib/sync.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export {
2020
CastingDateFunction,
2121
ColumnOption,
2222
Options,
23+
OptionsNormalized,
2324
Info,
2425
CsvErrorCode,
2526
CsvError,

0 commit comments

Comments
 (0)