]> git.ardis.lu - csvio/commitdiff
Support ArrayLike outputs
authorArdis Lu <ardisglu@gmail.com>
Sun, 6 Jul 2025 02:14:40 +0000 (19:14 -0700)
committerArdis Lu <ardisglu@gmail.com>
Sun, 6 Jul 2025 02:14:40 +0000 (19:14 -0700)
Could be useful for chained transformations using DOM APIs, e.g. passing through the NodeList object.

src/core.js
test/core.test.js

index 81e13ffbb036851384081259092002ce6623c9c1..d2bcc6abf32b2ad5fc38b00483675998006d5c68 100644 (file)
@@ -196,7 +196,7 @@ export class CSVReader extends ReadableStream {
 
 /**
  * A row or rows of output CSV data, or `null` to not output anything.
- * @typedef {Array<Array<any>>|Array<any>|string|null} TransformationOutput
+ * @typedef {Array<Array<any>>|Array<any>|ArrayLike<any>|string|null} TransformationOutput
  */
 
 /**
@@ -402,11 +402,16 @@ export class CSVTransformer extends TransformStream {
     else if (this.#rawOutput || typeof row === 'string') {
       controller.enqueue(row);
     }
-    else if (Array.isArray(row) && Array.isArray(row[0])) { // Multiple rows returned, enqueue each row separately
-      row.forEach(r => controller.enqueue(JSON.stringify(r)));
+    else if (Array.isArray(row)) {
+      if (Array.isArray(row[0])) { // Multiple rows returned, enqueue each row separately
+        row.forEach(r => controller.enqueue(JSON.stringify(r)));
+      }
+      else { // One row returned
+        controller.enqueue(JSON.stringify(row));
+      }
     }
-    else {
-      controller.enqueue(JSON.stringify(row));
+    else { // Assume ArrayLike output
+      controller.enqueue(JSON.stringify(Array.from(row)));
     }
   }
 
index 1c4918eb504665494cdb8ff3122196615092b7f6..3e0180ee7ffad1b14938a261d2b8e5e06c02a1fc 100644 (file)
@@ -327,6 +327,17 @@ suite('CSVTransformer', { concurrency: true }, () => {
         ['abc', 'def']
       ]));
   });
+  test('handles ArrayLike outputs', { concurrency: true }, async () => {
+    await createCSVMockStream([
+      ['columnA', 'columnB'],
+      ['a', 'b']
+    ])
+      .pipeThrough(new CSVTransformer(() => ({ length: 2, 0: 'a2', 1: 'b2' })))
+      .pipeTo(csvStreamEqualWritable([
+        ['columnA', 'columnB'],
+        ['a2', 'b2']
+      ]));
+  });
   test('consumes input row without output row when null is returned', { concurrency: true }, async () => {
     await createCSVMockStream([
       ['columnA', 'columnB'],
This page took 0.050123 seconds and 4 git commands to generate.