Skip to content

Commit

Permalink
feat(csv-demo-ts-module-node16): dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Aug 3, 2022
1 parent f66e6ea commit 8ed0e18
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions demo/ts-module-node16/lib/stringify-import.ts
@@ -0,0 +1,37 @@

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

(async function(){
const {stringify} = await import('csv-stringify');
console.log(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 8ed0e18

Please sign in to comment.