Skip to content

Commit

Permalink
feat(csv-demo-cjs): new stringify.ts sample
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Jul 10, 2022
1 parent 17e2d77 commit b44de05
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions demo/cjs/lib/stringify.ts
@@ -0,0 +1,32 @@

import assert from 'assert'
import { stringify, Stringifier } from 'csv-stringify';

let output: string = '';
// Create the parser
const stringifier: Stringifier = stringify({
delimiter: ':',
encoding: 'utf8'
});
// Use the readable stream api to consume records
stringifier.on('readable', function(){
let record; while ((record = stringifier.read()) !== null) {
output += record
}
});
// Catch any error
stringifier.on('error', function(err){
console.error(err.message)
});
// Test that the parsed records matched what's expected
stringifier.on('end', function(){
assert.deepStrictEqual(
output,
'a:b:c\n1:2:3\n'
)
});
// Write data to the stream
stringifier.write(["a", "b", "c"]);
stringifier.write([1, 2, 3]);
// Close the readable stream
stringifier.end();

0 comments on commit b44de05

Please sign in to comment.