11import "should" ;
2- import dedent from "dedent" ;
32import { parse } from "../lib/index.js" ;
43
54describe ( "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} ) ;
0 commit comments