Skip to content

Commit b44de05

Browse files
committed
feat(csv-demo-cjs): new stringify.ts sample
1 parent 17e2d77 commit b44de05

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

demo/cjs/lib/stringify.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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();

0 commit comments

Comments
 (0)