From: Ardis Lu Date: Mon, 27 Jan 2025 07:03:41 +0000 (-0800) Subject: Add an example of using CSVWriter status X-Git-Tag: v0.1.0~83 X-Git-Url: https://git.ardis.lu/?a=commitdiff_plain;h=a9e6888f362d6c1586ecef24e7d1105fee8e5f15;p=csvio Add an example of using CSVWriter status --- diff --git a/examples/README.md b/examples/README.md index 2140aa0..8d5dbe2 100644 --- a/examples/README.md +++ b/examples/README.md @@ -24,6 +24,8 @@ These examples demonstrate the most common, expected usage. [Example 1.5: Duplicate input rows to the output](./ex1_5.js) +[Example 1.6: Track the status of the CSV processing](./ex1_6.js) + ### Section 2: Error strategies These examples demonstrate using the `onError` option to handle transformations that may throw errors. diff --git a/examples/data/ex1_6-in.csv b/examples/data/ex1_6-in.csv new file mode 100644 index 0000000..d7edb82 --- /dev/null +++ b/examples/data/ex1_6-in.csv @@ -0,0 +1,11 @@ +columnA +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/examples/data/ex1_6-out.csv b/examples/data/ex1_6-out.csv new file mode 100644 index 0000000..d7edb82 --- /dev/null +++ b/examples/data/ex1_6-out.csv @@ -0,0 +1,11 @@ +columnA +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 diff --git a/examples/ex1_6.js b/examples/ex1_6.js new file mode 100644 index 0000000..28b80d6 --- /dev/null +++ b/examples/ex1_6.js @@ -0,0 +1,27 @@ +// ## Example 1.6: Track the status of the CSV processing +// +// Use the `status` property of CSVWriter to track the status of the output CSV file writing. This property +// updates as the writer receives data, so it may be used in a live progress tracker. + +import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js'; + +const writer = new CSVWriter(new URL('./data/ex1_6-out.csv', import.meta.url)); + +// For long-running processing, it may be useful to use an interval-based progress tracker as +// demonstrated below: +// const id = setInterval(() => { +// const { name, elapsed, rows } = writer.status; +// const message = `${name} | ${elapsed.toFixed()}ms | ${rows} rows written`; +// console.log(message); +// }, 1000); // Will log ongoing progress approximately every second + +await new CSVReader(new URL('./data/ex1_6-in.csv', import.meta.url)) + .pipeThrough(new CSVTransformer(r => r)) + .pipeTo(writer); + +const { name, rows, elapsed } = writer.status; +const message = `${name} | ${elapsed.toFixed()}ms | ${rows} rows written`; +console.log(message); + +// If using interval, remember to clear it after completion: +// clearInterval(id); \ No newline at end of file