]> git.ardis.lu - csvio/commitdiff
Add example to demonstrate passing a function to handleHeaders
authorArdis Lu <ardisglu@gmail.com>
Fri, 24 Jan 2025 04:21:43 +0000 (20:21 -0800)
committerArdis Lu <ardisglu@gmail.com>
Fri, 24 Jan 2025 04:21:43 +0000 (20:21 -0800)
examples/README.md
examples/data/ex1_5-in.csv [new file with mode: 0644]
examples/data/ex1_5-out.csv [new file with mode: 0644]
examples/ex1_5.js [new file with mode: 0644]

index 118c70331a5518b36b81472ef33ddd49e5a05904..6386a4c595cbf39e44b0f0c4bf30e993805a7695 100644 (file)
@@ -22,6 +22,8 @@ These examples demonstrate the most common, expected usage.
 
 [Example 1.4: Handling an irregularly-shaped CSV file](./ex1_4.js)
 
+[Example 1.5: Duplicate input rows to the output](./ex1_5.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_5-in.csv b/examples/data/ex1_5-in.csv
new file mode 100644 (file)
index 0000000..6347605
--- /dev/null
@@ -0,0 +1,4 @@
+a,b,c\r
+580,250,462\r
+325,950,740\r
+215,546,730\r
diff --git a/examples/data/ex1_5-out.csv b/examples/data/ex1_5-out.csv
new file mode 100644 (file)
index 0000000..4126884
--- /dev/null
@@ -0,0 +1,4 @@
+a,b,c,min,max,mean\r
+580,250,462,250,580,430.6666666666667\r
+325,950,740,325,950,671.6666666666666\r
+215,546,730,215,730,497\r
diff --git a/examples/ex1_5.js b/examples/ex1_5.js
new file mode 100644 (file)
index 0000000..0d49b52
--- /dev/null
@@ -0,0 +1,28 @@
+// ## Example 1.5: Duplicate input rows to the output
+// 
+// Use the spread syntax (...) to concisely duplicate input rows to the output, in both the transformation function
+// and the `handleHeaders` function.
+// 
+// Setting `handleHeaders` to a separate function is convenient to handle its logic separately from the data rows.
+// Using a transformation function instead of a hardcoded array is useful if the input row headers are unknown
+// or other arbitrary logic is required to determine the output header names.
+
+import { CSVReader, CSVTransformer, CSVWriter } from '../src/index.js';
+
+// It is useful to split the header transformation into a separate function because the logic for the header
+// is likely different from the rest of the CSV.
+function handleHeaders(headers) {
+  return [...headers, 'min', 'max', 'mean']; // Duplicate input headers as the first headers in the output
+}
+
+function calculate(row) {
+  const numbers = row.map(Number);
+  const min = Math.min(...numbers);
+  const max = Math.max(...numbers);
+  const mean = numbers.reduce((a, n) => a + n, 0) / numbers.length;
+  return [...row, min, max, mean]; // Duplicate input rows as the first columns in the output
+}
+
+await new CSVReader(new URL('./data/ex1_5-in.csv', import.meta.url))
+  .pipeThrough(new CSVTransformer(calculate, { handleHeaders }))
+  .pipeTo(new CSVWriter(new URL('./data/ex1_5-out.csv', import.meta.url)));
\ No newline at end of file
This page took 0.047557 seconds and 4 git commands to generate.