]> git.ardis.lu - csvio/commitdiff
Add an example of using CSVWriter status
authorArdis Lu <ardisglu@gmail.com>
Mon, 27 Jan 2025 07:03:41 +0000 (23:03 -0800)
committerArdis Lu <ardisglu@gmail.com>
Mon, 27 Jan 2025 07:03:41 +0000 (23:03 -0800)
examples/README.md
examples/data/ex1_6-in.csv [new file with mode: 0644]
examples/data/ex1_6-out.csv [new file with mode: 0644]
examples/ex1_6.js [new file with mode: 0644]

index 2140aa0c12309bc7bc91f2d87e98b23bec0ed23a..8d5dbe29054bb7fe22a70f3b6807260aea8765bb 100644 (file)
@@ -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 (file)
index 0000000..d7edb82
--- /dev/null
@@ -0,0 +1,11 @@
+columnA\r
+1\r
+2\r
+3\r
+4\r
+5\r
+6\r
+7\r
+8\r
+9\r
+10\r
diff --git a/examples/data/ex1_6-out.csv b/examples/data/ex1_6-out.csv
new file mode 100644 (file)
index 0000000..d7edb82
--- /dev/null
@@ -0,0 +1,11 @@
+columnA\r
+1\r
+2\r
+3\r
+4\r
+5\r
+6\r
+7\r
+8\r
+9\r
+10\r
diff --git a/examples/ex1_6.js b/examples/ex1_6.js
new file mode 100644 (file)
index 0000000..28b80d6
--- /dev/null
@@ -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
This page took 0.037974 seconds and 4 git commands to generate.