From 8ed0e186c9422ba5238b9e9c4adb9e94f8be4332 Mon Sep 17 00:00:00 2001 From: David Worms Date: Wed, 3 Aug 2022 22:48:44 +0200 Subject: [PATCH] feat(csv-demo-ts-module-node16): dynamic import --- demo/ts-module-node16/lib/stringify-import.ts | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 demo/ts-module-node16/lib/stringify-import.ts diff --git a/demo/ts-module-node16/lib/stringify-import.ts b/demo/ts-module-node16/lib/stringify-import.ts new file mode 100644 index 000000000..64a8a2ead --- /dev/null +++ b/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(); +})()