11import "should" ;
2- import { generate } from "csv-generate" ;
3- import { parse , Parser } from "../lib/index.js" ;
2+ import { parse } from "../lib/index.js" ;
43
54describe ( "API arguments" , function ( ) {
6- it ( "exports Parser class" , function ( ) {
7- Parser . should . be . a . Function ;
8- } ) ;
9-
10- describe ( "0 arg" , function ( ) {
11- it ( "no arguments" , function ( next ) {
12- const records = [ ] ;
13- const parser = parse ( ) ;
14- parser . on ( "readable" , function ( ) {
15- let d ;
16- while ( ( d = this . read ( ) ) ) {
17- records . push ( d ) ;
18- }
19- } ) ;
20- parser . on ( "err" , ( err ) => {
21- next ( err ) ;
22- } ) ;
23- parser . on ( "end" , ( ) => {
24- records . should . eql ( [
25- [ "field_1" , "field_2" ] ,
26- [ "value 1" , "value 2" ] ,
27- ] ) ;
28- next ( ) ;
29- } ) ;
30- parser . write ( "field_1,field_2\nvalue 1,value 2" ) ;
31- parser . end ( ) ;
32- } ) ;
33- } ) ;
34-
35- describe ( "1 arg" , function ( ) {
36- it ( "callback:function; pipe data and get result in callback" , function ( next ) {
37- generate ( { length : 2 , seed : 1 , columns : 2 , fixed_size : true } ) . pipe (
38- parse ( ( err , records ) => {
39- records . should . eql ( [
40- [ "OMH" , "ONKCHhJmjadoA" ] ,
41- [ "D" , "GeACHiN" ] ,
42- ] ) ;
43- next ( err ) ;
44- } ) ,
45- ) ;
46- } ) ;
47-
48- it ( "options:object; write data and read stream" , function ( next ) {
49- const records = [ ] ;
50- const parser = parse ( { columns : true } ) ;
51- parser . on ( "readable" , function ( ) {
52- let d ;
53- while ( ( d = parser . read ( ) ) ) {
54- records . push ( d ) ;
55- }
56- } ) ;
57- parser . on ( "err" , ( err ) => {
58- next ( err ) ;
59- } ) ;
60- parser . on ( "end" , ( ) => {
61- records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
62- next ( ) ;
63- } ) ;
64- parser . write ( "field_1,field_2\nvalue 1,value 2" ) ;
65- parser . end ( ) ;
66- } ) ;
67- } ) ;
68-
695 describe ( "2 args" , function ( ) {
70- it ( "data:string, options:object; read stream" , function ( next ) {
71- const records = [ ] ;
72- const parser = parse ( "field_1,field_2\nvalue 1,value 2" , {
73- columns : true ,
74- } ) ;
75- parser . on ( "readable" , function ( ) {
76- let d ;
77- while ( ( d = parser . read ( ) ) ) {
78- records . push ( d ) ;
79- }
80- } ) ;
81- parser . on ( "err" , ( err ) => {
82- next ( err ) ;
83- } ) ;
84- parser . on ( "end" , ( ) => {
85- records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
86- next ( ) ;
87- } ) ;
88- } ) ;
89-
90- it ( "options:object, callback:function; write data and get result in callback" , function ( next ) {
91- const parser = parse ( { columns : true } , ( err , records ) => {
92- records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
93- next ( err ) ;
94- } ) ;
95- parser . write ( "field_1,field_2\nvalue 1,value 2" ) ;
96- parser . end ( ) ;
97- } ) ;
98-
99- it ( "data:string, callback:function" , function ( next ) {
100- parse ( "value a,value b\nvalue 1,value 2" , ( err , records ) => {
101- records . should . eql ( [
102- [ "value a" , "value b" ] ,
103- [ "value 1" , "value 2" ] ,
104- ] ) ;
105- next ( err ) ;
106- } ) ;
107- } ) ;
108-
109- it ( "data:buffer, callback:function" , function ( next ) {
110- parse ( Buffer . from ( "value a,value b\nvalue 1,value 2" ) , ( err , records ) => {
111- records . should . eql ( [
112- [ "value a" , "value b" ] ,
113- [ "value 1" , "value 2" ] ,
114- ] ) ;
115- next ( err ) ;
116- } ) ;
117- } ) ;
118-
1196 it ( "data:undefined, options:object" , function ( ) {
1207 ( ( ) => {
1218 parse ( undefined , { } ) ;
@@ -155,28 +42,6 @@ describe("API arguments", function () {
15542 } ) ;
15643
15744 describe ( "3 args" , function ( ) {
158- it ( "data:string, options:object, callback:function" , function ( next ) {
159- parse (
160- "field_1,field_2\nvalue 1,value 2" ,
161- { columns : true } ,
162- ( err , records ) => {
163- records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
164- next ( err ) ;
165- } ,
166- ) ;
167- } ) ;
168-
169- it ( "data:buffer, options:object, callback:function" , function ( next ) {
170- parse (
171- Buffer . from ( "field_1,field_2\nvalue 1,value 2" ) ,
172- { columns : true } ,
173- ( err , records ) => {
174- records . should . eql ( [ { field_1 : "value 1" , field_2 : "value 2" } ] ) ;
175- next ( err ) ;
176- } ,
177- ) ;
178- } ) ;
179-
18045 it ( "data:undefined, options:object, callback:function" , function ( ) {
18146 ( ( ) => {
18247 parse ( undefined , { columns : true } , ( ) => { } ) ;
@@ -185,14 +50,5 @@ describe("API arguments", function () {
18550 code : "CSV_INVALID_ARGUMENT" ,
18651 } ) ;
18752 } ) ;
188-
189- it ( "data:string, options:object, callback:undefined" , function ( ) {
190- ( ( ) => {
191- parse ( "field_1,field_2\nvalue 1,value 2" , { columns : true } , undefined ) ;
192- } ) . should . throw ( {
193- message : "Invalid argument: got undefined at index 2" ,
194- code : "CSV_INVALID_ARGUMENT" ,
195- } ) ;
196- } ) ;
19753 } ) ;
19854} ) ;
0 commit comments