File tree Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Expand file tree Collapse file tree 1 file changed +32
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ import assert from 'assert'
3
+ import { stringify , Stringifier } from 'csv-stringify' ;
4
+
5
+ let output : string = '' ;
6
+ // Create the parser
7
+ const stringifier : Stringifier = stringify ( {
8
+ delimiter : ':' ,
9
+ encoding : 'utf8'
10
+ } ) ;
11
+ // Use the readable stream api to consume records
12
+ stringifier . on ( 'readable' , function ( ) {
13
+ let record ; while ( ( record = stringifier . read ( ) ) !== null ) {
14
+ output += record
15
+ }
16
+ } ) ;
17
+ // Catch any error
18
+ stringifier . on ( 'error' , function ( err ) {
19
+ console . error ( err . message )
20
+ } ) ;
21
+ // Test that the parsed records matched what's expected
22
+ stringifier . on ( 'end' , function ( ) {
23
+ assert . deepStrictEqual (
24
+ output ,
25
+ 'a:b:c\n1:2:3\n'
26
+ )
27
+ } ) ;
28
+ // Write data to the stream
29
+ stringifier . write ( [ "a" , "b" , "c" ] ) ;
30
+ stringifier . write ( [ 1 , 2 , 3 ] ) ;
31
+ // Close the readable stream
32
+ stringifier . end ( ) ;
You can’t perform that action at this time.
0 commit comments