Skip to content

Commit b9ac1f0

Browse files
committed
feat(csv-parse): boolean and null comment type
1 parent 89b34ed commit b9ac1f0

3 files changed

Lines changed: 144 additions & 137 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export interface Options<T = string[]> {
108108
/**
109109
* Treat all the characters after this one as a comment, default to '' (disabled).
110110
*/
111-
comment?: string;
111+
comment?: string | boolean | null;
112112
/**
113113
* Restrict the definition of comments to a full line. Comment characters
114114
* defined in the middle of the line are not interpreted as such. The
Lines changed: 0 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,8 @@
11
import "should";
2-
import dedent from "dedent";
32
import { parse } from "../lib/index.js";
43

54
describe("Option `comment`", function () {
65
it("validation", function () {
7-
parse("", { comment: undefined }, () => {});
8-
parse("", { comment: null }, () => {});
9-
parse("", { comment: false }, () => {});
10-
parse("", { comment: "" }, () => {});
11-
(() => {
12-
parse("", { comment: true }, () => {});
13-
}).should.throw({
14-
message:
15-
"Invalid option comment: comment must be a buffer or a string, got true",
16-
code: "CSV_INVALID_OPTION_COMMENT",
17-
});
186
(() => {
197
parse("", { comment: 2 }, () => {});
208
}).should.throw({
@@ -23,128 +11,4 @@ describe("Option `comment`", function () {
2311
code: "CSV_INVALID_OPTION_COMMENT",
2412
});
2513
});
26-
27-
it("single comment line", function (next) {
28-
parse("# comment", { comment: "#" }, (err, records) => {
29-
records.length.should.eql(0);
30-
next(err);
31-
});
32-
});
33-
34-
it("single comment line with empty field", function (next) {
35-
parse('""# comment', { comment: "#" }, (err, records) => {
36-
records.should.eql([[""]]);
37-
next(err);
38-
});
39-
});
40-
41-
it("skip line starting by single comment char", function (next) {
42-
parse(
43-
dedent`
44-
# skip this
45-
"ABC","45"
46-
"DEF","23"
47-
# and this
48-
"GHI","94"
49-
# as well as that
50-
`,
51-
{
52-
comment: "#",
53-
},
54-
(err, records) => {
55-
if (!err) {
56-
records.should.eql([
57-
["ABC", "45"],
58-
["DEF", "23"],
59-
["GHI", "94"],
60-
]);
61-
}
62-
next(err);
63-
},
64-
);
65-
});
66-
67-
it("doent apply inside quotes", function (next) {
68-
parse(
69-
dedent`
70-
"ABC","45"
71-
"D#noEF","23"#yes
72-
"GHI","94"
73-
`,
74-
{
75-
comment: "#",
76-
},
77-
(err, records) => {
78-
if (!err) {
79-
records.should.eql([
80-
["ABC", "45"],
81-
["D#noEF", "23"],
82-
["GHI", "94"],
83-
]);
84-
}
85-
next(err);
86-
},
87-
);
88-
});
89-
90-
it("is cancel if empty", function (next) {
91-
parse(
92-
dedent`
93-
abc,#,def
94-
1,2,3
95-
`,
96-
{
97-
comment: "",
98-
},
99-
(err, records) => {
100-
records.should.eql([
101-
["abc", "#", "def"],
102-
["1", "2", "3"],
103-
]);
104-
next();
105-
},
106-
);
107-
});
108-
109-
it("is cancel by default", function (next) {
110-
parse("abc,#,def\n1,2,3", (err, records) => {
111-
records.should.eql([
112-
["abc", "#", "def"],
113-
["1", "2", "3"],
114-
]);
115-
next();
116-
});
117-
});
118-
119-
it("accept multiple characters", function (next) {
120-
const parser = parse({ comment: "//" }, (err, records) => {
121-
records.should.eql([
122-
["abc", "def"],
123-
["1", "2"],
124-
]);
125-
next();
126-
});
127-
const data = dedent`
128-
abc,def
129-
// a comment
130-
1,2
131-
`;
132-
for (const char of data) {
133-
parser.write(char);
134-
}
135-
parser.end();
136-
});
137-
138-
it("accept quotes", function (next) {
139-
parse(
140-
dedent`
141-
"Alaska","Site1","Rack1","RTU-1","192.168.1.3"
142-
# Contains double-quote: "
143-
`,
144-
{
145-
comment: "#",
146-
},
147-
(err) => next(err),
148-
);
149-
});
15014
});
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import "should";
2+
import dedent from "dedent";
3+
import { parse } from "../lib/index.js";
4+
5+
describe("Option `comment`", function () {
6+
it("validation", function () {
7+
parse("", { comment: undefined }, () => {});
8+
parse("", { comment: null }, () => {});
9+
parse("", { comment: false }, () => {});
10+
parse("", { comment: "" }, () => {});
11+
(() => {
12+
parse("", { comment: true }, () => {});
13+
}).should.throw({
14+
message:
15+
"Invalid option comment: comment must be a buffer or a string, got true",
16+
code: "CSV_INVALID_OPTION_COMMENT",
17+
});
18+
});
19+
20+
it("single comment line", function (next) {
21+
parse("# comment", { comment: "#" }, (err, records) => {
22+
records.length.should.eql(0);
23+
next(err);
24+
});
25+
});
26+
27+
it("single comment line with empty field", function (next) {
28+
parse('""# comment', { comment: "#" }, (err, records) => {
29+
records.should.eql([[""]]);
30+
next(err);
31+
});
32+
});
33+
34+
it("skip line starting by single comment char", function (next) {
35+
parse(
36+
dedent`
37+
# skip this
38+
"ABC","45"
39+
"DEF","23"
40+
# and this
41+
"GHI","94"
42+
# as well as that
43+
`,
44+
{
45+
comment: "#",
46+
},
47+
(err, records) => {
48+
if (!err) {
49+
records.should.eql([
50+
["ABC", "45"],
51+
["DEF", "23"],
52+
["GHI", "94"],
53+
]);
54+
}
55+
next(err);
56+
},
57+
);
58+
});
59+
60+
it("doent apply inside quotes", function (next) {
61+
parse(
62+
dedent`
63+
"ABC","45"
64+
"D#noEF","23"#yes
65+
"GHI","94"
66+
`,
67+
{
68+
comment: "#",
69+
},
70+
(err, records) => {
71+
if (!err) {
72+
records.should.eql([
73+
["ABC", "45"],
74+
["D#noEF", "23"],
75+
["GHI", "94"],
76+
]);
77+
}
78+
next(err);
79+
},
80+
);
81+
});
82+
83+
it("is cancel if empty", function (next) {
84+
parse(
85+
dedent`
86+
abc,#,def
87+
1,2,3
88+
`,
89+
{
90+
comment: "",
91+
},
92+
(err, records) => {
93+
records.should.eql([
94+
["abc", "#", "def"],
95+
["1", "2", "3"],
96+
]);
97+
next();
98+
},
99+
);
100+
});
101+
102+
it("is cancel by default", function (next) {
103+
parse("abc,#,def\n1,2,3", (err, records) => {
104+
records.should.eql([
105+
["abc", "#", "def"],
106+
["1", "2", "3"],
107+
]);
108+
next();
109+
});
110+
});
111+
112+
it("accept multiple characters", function (next) {
113+
const parser = parse({ comment: "//" }, (err, records) => {
114+
records.should.eql([
115+
["abc", "def"],
116+
["1", "2"],
117+
]);
118+
next();
119+
});
120+
const data = dedent`
121+
abc,def
122+
// a comment
123+
1,2
124+
`;
125+
for (const char of data) {
126+
parser.write(char);
127+
}
128+
parser.end();
129+
});
130+
131+
it("accept quotes", function (next) {
132+
parse(
133+
dedent`
134+
"Alaska","Site1","Rack1","RTU-1","192.168.1.3"
135+
# Contains double-quote: "
136+
`,
137+
{
138+
comment: "#",
139+
},
140+
(err) => next(err),
141+
);
142+
});
143+
});

0 commit comments

Comments
 (0)