]> git.ardis.lu - csvio/commitdiff
Clarify handleHeaders intended usage
authorArdis Lu <ardisglu@gmail.com>
Fri, 24 Jan 2025 04:23:21 +0000 (20:23 -0800)
committerArdis Lu <ardisglu@gmail.com>
Fri, 24 Jan 2025 04:23:21 +0000 (20:23 -0800)
Setting it to an Array<any> is preferred for simple scenarios.

examples/README.md
examples/data/ex1_2-out.csv
examples/ex1_2.js
examples/ex1_3.js
examples/ex3_2.js

index 6386a4c595cbf39e44b0f0c4bf30e993805a7695..dc9abea7201755beb04b3e2fda84d94b3518b00a 100644 (file)
@@ -16,7 +16,7 @@ These examples demonstrate the most common, expected usage.
 
 [Example 1.1: Basic usage](./ex1_1.js)
 
-[Example 1.2: Updating headers in the output](./ex1_2.js)
+[Example 1.2: Replace headers in the output](./ex1_2.js)
 
 [Example 1.3: Chaining multiple transformations](./ex1_3.js)
 
index 69e4d657f777d6a9f6d041ad8f889d0349953864..61b037405fc7dbd2571277d721b0942cc6396c84 100644 (file)
@@ -1,4 +1,4 @@
-columnA,columnB,sum\r
+Column A,Column B,Sum\r
 1,1,2\r
 100,100,200\r
 223423,455947,679370\r
index cf8a892a6ba442f35496c3bda313af6660dd3005..68ac8640fa85e689d408c971af1ca1a3cb61713f 100644 (file)
@@ -1,23 +1,17 @@
-// ## Example 1.2: Updating headers in the output
+// ## Example 1.2: Replace headers in the output
 // 
-// Set `handleHeaders: true` to manually handle the header row (first row).
+// Set `handleHeaders` to an `Array<any>` to replace the header row (first row) in the output.
 // 
 // Notes:
 // - The header row is assumed to be the first row in the input CSV, and by default it goes directly to the output (skipping the user-provided transformation function).
-// - If your input CSV does not have a header row, set `handleHeaders: true` and process the header row (first row) normally.
-// - Otherwise, you will need distinct logic to handle the header row vs. data rows. A simple boolean variable is a good way to switch between this logic. 
+// - Setting `handleHeaders` to an array is the simplest way to rename headers, delete headers, or add new headers.
 
 import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
 
-let firstRow = true;
 function sum(row) {
-  if (firstRow) {
-    firstRow = false;
-    return [...row, 'sum'];
-  }
   return [...row, Number(row[0]) + Number(row[1])];
 }
 
 await new CSVReader(new URL('./data/ex1_2-in.csv', import.meta.url))
-  .pipeThrough(new CSVTransformer(sum, { handleHeaders: true }))
+  .pipeThrough(new CSVTransformer(sum, { handleHeaders: ['Column A', 'Column B', 'Sum'] }))
   .pipeTo(new CSVWriter(new URL('./data/ex1_2-out.csv', import.meta.url)));
\ No newline at end of file
index 7fdc65b23c5dd035ef2669edc283b2e8cd769553..c19486f087fd3aabc0f75af683dd0688d219f746 100644 (file)
@@ -4,6 +4,9 @@
 // (`rawOutput`) when handling the transformation function input/output. These options are intended for advanced
 // transformations (e.g., transformations spread across multiple functions or lower-level transformations directly
 // on byte arrays).
+// 
+// Set `handleHeaders` to `true` to pass the header row to the transformation function as a normal row and manually
+// handle the header output processing. This setting is required if the input CSV does not have a header row.
 
 import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
 
index caeadcaa9b216cf723efd145ab55677c2281fada..bf341b60833f17c63d576ab8e535db28a18419a0 100644 (file)
@@ -17,16 +17,11 @@ function tick() {
   return Math.floor(ms / 100);
 }
 
-let firstChunk = true;
 async function concurrent(row) {
-  if (firstChunk) {
-    firstChunk = false;
-    return ['tick number', ...row];
-  }
   await setTimeout(100); // Simulate some slow task, e.g. a network request
   return [tick(), ...row];
 }
 
 await new CSVReader(new URL('./data/ex3_2-in.csv', import.meta.url))
-  .pipeThrough(new CSVTransformer(concurrent, { handleHeaders: true, maxConcurrent: 5 }))
+  .pipeThrough(new CSVTransformer(concurrent, { handleHeaders: h => ['tick number', ...h], maxConcurrent: 5 }))
   .pipeTo(new CSVWriter(new URL('./data/ex3_2-out.csv', import.meta.url)));
\ No newline at end of file
This page took 0.048015 seconds and 4 git commands to generate.