From b44de05d70b4577cdb85c4f285321eea840f8fa3 Mon Sep 17 00:00:00 2001 From: David Worms Date: Sun, 10 Jul 2022 22:33:30 +0200 Subject: [PATCH] feat(csv-demo-cjs): new stringify.ts sample --- demo/cjs/lib/stringify.ts | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 demo/cjs/lib/stringify.ts diff --git a/demo/cjs/lib/stringify.ts b/demo/cjs/lib/stringify.ts new file mode 100644 index 000000000..f4cf1db94 --- /dev/null +++ b/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();