Skip to content

Commit cf96584

Browse files
committed
test(csv-parse): options ts convertion
1 parent da7a62e commit cf96584

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+4317
-3925
lines changed

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

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -370,33 +370,33 @@ export interface Options<T = string[]> {
370370
/**
371371
* Generate two properties raw and row where raw is the original CSV row content and row is the parsed array or object.
372372
*/
373-
raw?: boolean;
373+
raw?: boolean | null;
374+
/**
375+
* One or multiple characters used to delimit record rows; defaults to auto discovery if not provided.
376+
* Supported auto discovery method are Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters.
377+
*/
378+
record_delimiter?: string | Buffer | null | (string | Buffer | null)[];
379+
recordDelimiter?: string | Buffer | null | (string | Buffer | null)[];
374380
/**
375381
* Discard inconsistent columns count, default to false.
376382
*/
377-
relax_column_count?: boolean;
378-
relaxColumnCount?: boolean;
383+
relax_column_count?: boolean | null;
384+
relaxColumnCount?: boolean | null;
379385
/**
380386
* Discard inconsistent columns count when the record contains less fields than expected, default to false.
381387
*/
382-
relax_column_count_less?: boolean;
383-
relaxColumnCountLess?: boolean;
388+
relax_column_count_less?: boolean | null;
389+
relaxColumnCountLess?: boolean | null;
384390
/**
385391
* Discard inconsistent columns count when the record contains more fields than expected, default to false.
386392
*/
387-
relax_column_count_more?: boolean;
388-
relaxColumnCountMore?: boolean;
393+
relax_column_count_more?: boolean | null;
394+
relaxColumnCountMore?: boolean | null;
389395
/**
390396
* Preserve quotes inside unquoted field.
391397
*/
392-
relax_quotes?: boolean;
393-
relaxQuotes?: boolean;
394-
/**
395-
* One or multiple characters used to delimit record rows; defaults to auto discovery if not provided.
396-
* Supported auto discovery method are Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters.
397-
*/
398-
record_delimiter?: string | string[] | Buffer | Buffer[];
399-
recordDelimiter?: string | string[] | Buffer | Buffer[];
398+
relax_quotes?: boolean | null;
399+
relaxQuotes?: boolean | null;
400400
/**
401401
* If true, ignore whitespace immediately preceding the delimiter (i.e. right-trim all fields), defaults to false.
402402
* Does not remove whitespace in a quoted field.
@@ -406,27 +406,27 @@ export interface Options<T = string[]> {
406406
* Dont generate empty values for empty lines.
407407
* Defaults to false
408408
*/
409-
skip_empty_lines?: boolean;
410-
skipEmptyLines?: boolean;
409+
skip_empty_lines?: boolean | null;
410+
skipEmptyLines?: boolean | null;
411411
/**
412-
* Skip a line with error found inside and directly go process the next line.
412+
* Don't generate records for lines containing empty column values (column matching /\s*\/), defaults to false.
413413
*/
414-
skip_records_with_error?: boolean;
415-
skipRecordsWithError?: boolean;
414+
skip_records_with_empty_values?: boolean | null;
415+
skipRecordsWithEmptyValues?: boolean | null;
416416
/**
417-
* Don't generate records for lines containing empty column values (column matching /\s*\/), defaults to false.
417+
* Skip a line with error found inside and directly go process the next line.
418418
*/
419-
skip_records_with_empty_values?: boolean;
420-
skipRecordsWithEmptyValues?: boolean;
419+
skip_records_with_error?: boolean | null;
420+
skipRecordsWithError?: boolean | null;
421421
/**
422422
* Stop handling records after the requested number of records.
423423
*/
424-
to?: number;
424+
to?: null | number | string;
425425
/**
426426
* Stop handling records after the requested line number.
427427
*/
428-
to_line?: number;
429-
toLine?: number;
428+
to_line?: null | number | string;
429+
toLine?: null | number | string;
430430
/**
431431
* If true, ignore whitespace immediately around the delimiter, defaults to false.
432432
* Does not remove whitespace in a quoted field.

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

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

44
/* eslint mocha/no-exports: "off" */
55
export const assert_error = function <T>(
@@ -60,12 +60,10 @@ describe("API assert_error", function () {
6060
});
6161

6262
it("exhaustive detect a property not in assert", function () {
63-
const err = new CsvError(
64-
"CSV_UNKNOWN_ERROR",
65-
"A message",
66-
{},
67-
{ a_key: "a value" },
68-
);
63+
const options = normalize_options({});
64+
const err = new CsvError("CSV_UNKNOWN_ERROR", "A message", options, {
65+
a_key: "a value",
66+
});
6967
(() => {
7068
assert_error(
7169
err,
@@ -124,12 +122,10 @@ describe("API assert_error", function () {
124122
});
125123

126124
it("validate a boolean true value", function () {
127-
const err = new CsvError(
128-
"CSV_UNKNOWN_ERROR",
129-
"A message",
130-
{},
131-
{ a_boolean: true },
132-
);
125+
const options = normalize_options({});
126+
const err = new CsvError("CSV_UNKNOWN_ERROR", "A message", options, {
127+
a_boolean: true,
128+
});
133129
assert_error<{ a_boolean: boolean }>(err, {
134130
a_boolean: true,
135131
});
@@ -141,12 +137,10 @@ describe("API assert_error", function () {
141137
});
142138

143139
it("validate a boolean false value", function () {
144-
const err = new CsvError(
145-
"CSV_UNKNOWN_ERROR",
146-
"A message",
147-
{},
148-
{ a_boolean: false },
149-
);
140+
const options = normalize_options({});
141+
const err = new CsvError("CSV_UNKNOWN_ERROR", "A message", options, {
142+
a_boolean: false,
143+
});
150144
assert_error(err, {
151145
a_boolean: false,
152146
});

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Options,
77
Parser,
88
CsvError,
9+
normalize_options,
910
} from "../lib/index.js";
1011

1112
describe("API Types", function () {
@@ -30,7 +31,7 @@ describe("API Types", function () {
3031

3132
it("Expose options", function () {
3233
const parser: Parser = parse();
33-
const options: Options = parser.options;
34+
const options = parser.options;
3435
const keys: string[] = Object.keys(options);
3536
keys
3637
.sort()
@@ -224,13 +225,15 @@ describe("API Types", function () {
224225

225226
const typedOptions: Options<Person> = {};
226227
typedOptions.columns = ["age", undefined, null, false, { name: "name" }];
227-
typedOptions.columns = (record) => {
228+
typedOptions.columns = (record: Person) => {
229+
record;
228230
return ["age"];
229231
};
230232

231233
const unknownTypedOptions: Options<unknown> = {};
232234
unknownTypedOptions.columns = ["anything", undefined, null, false];
233-
unknownTypedOptions.columns = (record) => {
235+
unknownTypedOptions.columns = (record: unknown) => {
236+
record;
234237
return ["anything", undefined, null, false];
235238
};
236239
});
@@ -429,10 +432,11 @@ describe("API Types", function () {
429432
});
430433

431434
it("Supports contexts", function () {
435+
const options = normalize_options({});
432436
const error = new CsvError(
433437
"CSV_RECORD_INCONSISTENT_FIELDS_LENGTH",
434438
"MESSAGE",
435-
{},
439+
options,
436440
{ testContext: { testProp: "testValue" } },
437441
);
438442

@@ -443,12 +447,11 @@ describe("API Types", function () {
443447
});
444448

445449
it("Proper type is thrown when an error is encountered", function () {
446-
parse(`a,b\nc`, function (e: Error | undefined) {
450+
parse(`a,b\nc`, function (e) {
451+
if (!e) throw Error("Invalid assessment");
447452
const isCsvError = e instanceof CsvError;
448453
isCsvError.should.be.true();
449-
(e as CsvError).code.should.eql(
450-
"CSV_RECORD_INCONSISTENT_FIELDS_LENGTH",
451-
);
454+
e.code.should.eql("CSV_RECORD_INCONSISTENT_FIELDS_LENGTH");
452455
});
453456
});
454457
});

packages/csv-parse/test/info.lines.js renamed to packages/csv-parse/test/info.lines.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ describe("info lines", function () {
4747
"and",valid,line,follows...
4848
`,
4949
(err, records) => {
50+
if (!err) return next(Error("Invalid assessment"));
5051
assert_error(err, {
5152
message:
5253
'Invalid Opening Quote: a quote is found on field 4 at line 3, value is "h"',
@@ -72,6 +73,7 @@ describe("info lines", function () {
7273
skip_empty_lines: true,
7374
},
7475
(err, records) => {
76+
if (!err) return next(Error("Invalid assessment"));
7577
assert_error(err, {
7678
message:
7779
'Invalid Opening Quote: a quote is found on field 3 at line 4, value is "invalid h"',
@@ -94,6 +96,7 @@ describe("info lines", function () {
9496
"",1974,8.8392926E7,"",""
9597
`,
9698
(err, records) => {
99+
if (!err) return next(Error("Invalid assessment"));
97100
assert_error(err, {
98101
message:
99102
"Quote Not Closed: the parsing is finished with an opening quote at line 5",
@@ -120,6 +123,7 @@ describe("info lines", function () {
120123
delimiter: "\t",
121124
},
122125
(err, records) => {
126+
if (!err) return next(Error("Invalid assessment"));
123127
assert_error(err, {
124128
message:
125129
'Invalid Closing Quote: got " " at line 3 instead of delimiter, record delimiter, trimable character (if activated) or comment',
@@ -146,6 +150,7 @@ describe("info lines", function () {
146150
escape: '"',
147151
},
148152
(err, records) => {
153+
if (!err) return next(Error("Invalid assessment"));
149154
assert_error(err, {
150155
message:
151156
'Invalid Closing Quote: got "t" at line 2 instead of delimiter, record delimiter, trimable character (if activated) or comment',

packages/csv-parse/test/option.bom.js

Lines changed: 0 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import "should";
22
import { parse } from "../lib/index.js";
3-
import { assert_error } from "./api.assert_error.js";
43

54
describe("Option `bom`", function () {
65
it("validate", function () {
@@ -11,126 +10,4 @@ describe("Option `bom`", function () {
1110
code: "CSV_INVALID_OPTION_BOM",
1211
});
1312
});
14-
15-
it("preserve bom if not defined", function (next) {
16-
const parser = parse((err, records) => {
17-
records.should.eql([
18-
["\ufeffa", "b", "c"],
19-
["d", "e", "f"],
20-
]);
21-
next();
22-
});
23-
parser.write(Buffer.from("\ufeffa,b,c\n"));
24-
parser.write(Buffer.from("d,e,f"));
25-
parser.end();
26-
});
27-
28-
it("preserve BOM if false", function (next) {
29-
const parser = parse({ bom: false }, (err, records) => {
30-
records.should.eql([
31-
["\ufeffa", "b", "c"],
32-
["d", "e", "f"],
33-
]);
34-
next();
35-
});
36-
parser.write(Buffer.from("\ufeffa,b,c\n"));
37-
parser.write(Buffer.from("d,e,f"));
38-
parser.end();
39-
});
40-
41-
it("with column option with bom `true`", function (next) {
42-
const parser = parse(
43-
{
44-
columns: true,
45-
bom: true,
46-
},
47-
(err, records) => {
48-
records[0]["key"].should.eql("value");
49-
next();
50-
},
51-
);
52-
parser.write(Buffer.from("\ufeffkey\nvalue"));
53-
parser.end();
54-
});
55-
56-
it("with column option with bom `false`", function (next) {
57-
const parser = parse(
58-
{
59-
columns: true,
60-
bom: false,
61-
},
62-
(err, records) => {
63-
records[0]["\ufeffkey"].should.eql("value");
64-
next();
65-
},
66-
);
67-
parser.write(Buffer.from("\ufeffkey\nvalue"));
68-
parser.end();
69-
});
70-
71-
it("throw parsing error if quote follow bom", function (next) {
72-
const parser = parse((err) => {
73-
assert_error(err, {
74-
message:
75-
'Invalid Opening Quote: a quote is found on field 0 at line 1, value is "\ufeff" (utf8 bom)',
76-
code: "INVALID_OPENING_QUOTE",
77-
field: "\ufeff",
78-
});
79-
next();
80-
});
81-
parser.write(Buffer.from('\ufeff"a",b,c\n'));
82-
parser.write(Buffer.from("d,e,f"));
83-
parser.end();
84-
});
85-
86-
it("handle BOM with utf8 (default)", function (next) {
87-
const parser = parse({ bom: true }, (err, records) => {
88-
records.should.eql([
89-
["a", "b", "c"],
90-
["d", "e", "f"],
91-
]);
92-
next();
93-
});
94-
parser.write(Buffer.from("\ufeffa,b,c\n"));
95-
parser.write(Buffer.from("d,e,f"));
96-
parser.end();
97-
});
98-
99-
it("preserve data if BOM is true", function (next) {
100-
const parser = parse({ bom: true }, (err, records) => {
101-
records.should.eql([
102-
["a", "b", "c"],
103-
["d", "e", "f"],
104-
]);
105-
next();
106-
});
107-
parser.write(Buffer.from("a,b,c\n"));
108-
parser.write(Buffer.from("d,e,f"));
109-
parser.end();
110-
});
111-
112-
it("handle BOM even if no enough data in the first package", function (next) {
113-
const parser = parse({ bom: true }, (err, records) => {
114-
records.should.eql([
115-
["a", "b", "c"],
116-
["d", "e", "f"],
117-
]);
118-
next();
119-
});
120-
parser.write(Buffer.from([239]));
121-
parser.write(Buffer.from([187]));
122-
parser.write(Buffer.from([191]));
123-
parser.write(Buffer.from("a,b,c\n"));
124-
parser.write(Buffer.from("d,e,f"));
125-
parser.end();
126-
});
127-
128-
it("preserve data if no enough data to detect BOM", function (next) {
129-
const parser = parse({ bom: true }, (err, records) => {
130-
records.should.eql([["\ufffd"]]);
131-
next();
132-
});
133-
parser.write(Buffer.from([239, 187]));
134-
parser.end();
135-
});
13613
});

0 commit comments

Comments
 (0)