Skip to content

Commit 18bb78a

Browse files
committed
feat(csv-parse): alig info interfaces with js api
1 parent 53a80a3 commit 18bb78a

File tree

6 files changed

+59
-39
lines changed

6 files changed

+59
-39
lines changed

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

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import * as stream from "stream";
77
export type Callback<T = string[]> = (
88
err: CsvError | undefined,
99
records: T[],
10-
info?: Info,
10+
info?: InfoCallback,
1111
) => void;
1212

1313
export class Parser extends stream.Transform {
@@ -25,59 +25,66 @@ export class Parser extends stream.Transform {
2525

2626
export interface Info {
2727
/**
28-
* Count the number of lines being fully commented.
28+
* The number of processed bytes.
29+
*/
30+
readonly bytes: number;
31+
/**
32+
* The number of processed bytes until the last successfully parsed and emitted records.
33+
*/
34+
readonly bytes_records: number;
35+
/**
36+
* The number of lines being fully commented.
2937
*/
3038
readonly comment_lines: number;
3139
/**
32-
* Count the number of processed empty lines.
40+
* The number of processed empty lines.
3341
*/
3442
readonly empty_lines: number;
43+
/**
44+
* The number of non uniform records when `relax_column_count` is true.
45+
*/
46+
readonly invalid_field_length: number;
3547
/**
3648
* The number of lines encountered in the source dataset, start at 1 for the first line.
3749
*/
3850
readonly lines: number;
3951
/**
40-
* Count the number of processed records.
52+
* The number of processed records.
4153
*/
4254
readonly records: number;
43-
/**
44-
* Count of the number of processed bytes.
45-
*/
46-
readonly bytes: number;
47-
/**
48-
* Number of non uniform records when `relax_column_count` is true.
49-
*/
50-
readonly invalid_field_length: number;
55+
}
56+
57+
export interface InfoCallback extends Info {
5158
/**
5259
* Normalized verion of `options.columns` when `options.columns` is true, boolean otherwise.
5360
*/
5461
readonly columns: boolean | { name: string }[] | { disabled: true }[];
5562
}
5663

57-
export interface CastingContext {
64+
export interface InfoDataSet extends Info {
5865
readonly column: number | string;
59-
readonly bytes: number;
60-
readonly bytes_records: number;
61-
readonly empty_lines: number;
66+
}
67+
68+
export interface InfoRecord extends InfoDataSet {
6269
readonly error: CsvError;
6370
readonly header: boolean;
6471
readonly index: number;
65-
readonly quoting: boolean;
66-
readonly lines: number;
6772
readonly raw: string | undefined;
68-
readonly records: number;
69-
readonly invalid_field_length: number;
7073
}
7174

72-
export type CastingFunction = (
73-
value: string,
74-
context: CastingContext,
75-
) => unknown;
75+
export interface InfoField extends InfoRecord {
76+
readonly quoting: boolean;
77+
}
78+
79+
/**
80+
* @deprecated Use the InfoField interface instead, the interface will disappear in future versions.
81+
*/
82+
// eslint-disable-next-line
83+
export interface CastingContext extends InfoField {}
84+
85+
export type CastingFunction = (value: string, context: InfoField) => unknown;
7686

77-
export type CastingDateFunction = (
78-
value: string,
79-
context: CastingContext,
80-
) => Date;
87+
export type CastingDateFunction = (value: string, context: InfoField) => Date;
8188

8289
export type ColumnOption<K = string> =
8390
| K
@@ -183,7 +190,7 @@ export interface OptionsNormalized<T = string[]> {
183190
/**
184191
* Alter and filter records by executing a user defined function.
185192
*/
186-
on_record?: (record: T, context: CastingContext) => T | undefined;
193+
on_record?: (record: T, context: InfoRecord) => T | undefined;
187194
/**
188195
* Optional character surrounding a field, one character only, defaults to double quotes.
189196
*/
@@ -359,8 +366,8 @@ export interface Options<T = string[]> {
359366
/**
360367
* Alter and filter records by executing a user defined function.
361368
*/
362-
on_record?: (record: T, context: CastingContext) => T | null | undefined;
363-
onRecord?: (record: T, context: CastingContext) => T | null | undefined;
369+
on_record?: (record: T, context: InfoRecord) => T | null | undefined;
370+
onRecord?: (record: T, context: InfoRecord) => T | null | undefined;
364371
/**
365372
* Function called when an error occured if the `skip_records_with_error`
366373
* option is activated.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ declare function parse(options?: Options): TransformStream;
55
export { parse };
66

77
export {
8-
CastingContext,
8+
CastingContext, // Deprecated
99
CastingFunction,
1010
CastingDateFunction,
1111
ColumnOption,
1212
Options,
1313
OptionsNormalized,
1414
Info,
15+
InfoCallback,
16+
InfoDataSet,
17+
InfoRecord,
18+
InfoField,
1519
CsvErrorCode,
1620
CsvError,
1721
} from "./index.js";

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@ declare function parse(input: Buffer | string | Uint8Array): string[][];
1818
export { parse };
1919

2020
export {
21-
CastingContext,
21+
CastingContext, // Deprecated
2222
CastingFunction,
2323
CastingDateFunction,
2424
ColumnOption,
2525
Options,
2626
OptionsNormalized,
2727
Info,
28+
InfoCallback,
29+
InfoDataSet,
30+
InfoRecord,
31+
InfoField,
2832
CsvErrorCode,
2933
CsvError,
3034
} from "./index.js";

packages/csv-parse/test/api.types.sync.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ describe("API Types", function () {
8989
it("Info", function () {
9090
const info: Info = {
9191
bytes: 1,
92-
columns: true,
92+
bytes_records: 0,
93+
// columns: true,
9394
comment_lines: 1,
9495
empty_lines: 1,
9596
invalid_field_length: 1,

packages/csv-parse/test/api.types.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ describe("API Types", function () {
104104
describe("Info", function () {
105105
const fakeinfo = {
106106
bytes: 1,
107+
bytes_records: 0,
107108
columns: true,
108109
comment_lines: 1,
109110
empty_lines: 1,
@@ -152,7 +153,8 @@ describe("API Types", function () {
152153
// Boolean
153154
const infoBoolean: Info = {
154155
bytes: 1,
155-
columns: true,
156+
bytes_records: 0,
157+
// columns: true,
156158
comment_lines: 1,
157159
empty_lines: 1,
158160
invalid_field_length: 1,
@@ -163,7 +165,8 @@ describe("API Types", function () {
163165
// Array with name = <string>
164166
const infoName: Info = {
165167
bytes: 1,
166-
columns: [{ name: "a column" }],
168+
bytes_records: 0,
169+
// columns: [{ name: "a column" }],
167170
comment_lines: 1,
168171
empty_lines: 1,
169172
invalid_field_length: 1,
@@ -174,7 +177,8 @@ describe("API Types", function () {
174177
// Array with disabled = true
175178
const infoDisabled: Info = {
176179
bytes: 1,
177-
columns: [{ disabled: true }],
180+
bytes_records: 0,
181+
// columns: [{ disabled: true }],
178182
comment_lines: 1,
179183
empty_lines: 1,
180184
invalid_field_length: 1,

packages/csv-parse/test/option.on_record.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import should from "should";
2-
import { parse, CastingContext } from "../lib/index.js";
2+
import { parse, InfoRecord } from "../lib/index.js";
33

44
describe("Option `on_record`", function () {
55
describe("usage", function () {
@@ -161,7 +161,7 @@ describe("Option `on_record`", function () {
161161
});
162162

163163
it("values", function (next) {
164-
parse<CastingContext>(
164+
parse<InfoRecord>(
165165
"a,b\n1,2\n3,4",
166166
{
167167
on_record: (record, context) => {

0 commit comments

Comments
 (0)