]> git.ardis.lu - csvio/commitdiff
Update `maxBatchSize` behavior to match type documentation
authorArdis Lu <ardisglu@gmail.com>
Wed, 28 May 2025 07:26:30 +0000 (00:26 -0700)
committerArdis Lu <ardisglu@gmail.com>
Wed, 28 May 2025 07:26:30 +0000 (00:26 -0700)
**Any** value should start batching, instead of any value > 1.

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

index 596cd590f565071d20a82f417f1dd4991b6a2743..9749f6e118b95cfbf3d99c5df296027222f83119 100644 (file)
@@ -272,9 +272,7 @@ export class CSVReader extends ReadableStream {
  * input data, the error that was thrown, and the transformation function itself will be passed to the `onError` function. The default value is
  * `null` (errors will not be caught).
  * @property {number} [maxBatchSize=1] Set to the maximum number of rows that will be passed to the transformation function per function call
- * (greedy). The default value is `1`.
- * 
- * NOTE: If this value is set to any value (including `1`), then value passed to `fn()` will be a 2-D array of type `Array<Array<string>>`.
+ * (greedy).
  * @property {number} [maxConcurrent=1] The maximum concurrent executions of the transformation function (greedy). The transformation function
  * will be automatically turned into a promise if it isn't already async. Execution is blocked until all promises in a concurrent group settle
  * (i.e., if one transformation in a group is hanging, further rows will NOT be processed even if all other transformations in the group are
@@ -298,7 +296,7 @@ export class CSVReader extends ReadableStream {
  * to catch errors thrown by the transformation function. The input data, the error that was thrown, and the transformation function itself will
  * be passed to the `onError` function. The default value is `null` (errors will not be caught).
  * @property {number} maxBatchSize Set to the maximum number of rows that will be passed to the transformation function per function call
- * (greedy). The default value is `1`.
+ * (greedy).
  * 
  * NOTE: If this value is set to any value (including `1`), then value passed to `fn()` will be a 2-D array of type `Array<Array<string>>`.
  * @property {number} [maxConcurrent=1] The maximum concurrent executions of the transformation function (greedy). The transformation function
@@ -357,7 +355,7 @@ export class CSVTransformer extends TransformStream {
     this.#rawInput = options.rawInput ?? false;
     this.#rawOutput = options.rawOutput ?? false;
     this.#onError = options.onError ?? null;
-    this.#maxBatchSize = options.maxBatchSize ?? 1;
+    this.#maxBatchSize = options.maxBatchSize ?? null;
     this.#maxConcurrent = options.maxConcurrent ?? 1;
 
     this.#fn = fn;
@@ -424,7 +422,7 @@ export class CSVTransformer extends TransformStream {
       return;
     }
 
-    if (this.#maxBatchSize > 1) {
+    if (this.#maxBatchSize !== null) {
       this.#batch.push(row);
       if (this.#batch.length === this.#maxBatchSize) {
         this.#concurrent.push(this.#wrappedFn(this.#batch));
index 5f6dc8fcc81a733d846deb3fcfc9f3185f40d13e..1fe7f5bde0539439c9a3a1fe550d6b55b862fca9 100644 (file)
@@ -441,6 +441,21 @@ suite('CSVTransformer', { concurrency: true }, () => {
       { name: 'Error', message: 'Not an error type' }
     );
   });
+  test('creates batch of 1 when maxBatchSize is equal to 1', { concurrency: true }, async () => {
+    await createCSVMockStream([
+      ['columnA', 'columnB'],
+      ['1', '2'],
+      ['3', '4'],
+      ['5', '6']
+    ])
+      .pipeThrough(new CSVTransformer(b => b.map(r => r.map(f => Number(f) * 2)), { maxBatchSize: 1 }))
+      .pipeTo(csvStreamEqualWritable([
+        ['columnA', 'columnB'],
+        [2, 4],
+        [6, 8],
+        [10, 12]
+      ]));
+  });
   test('creates one batch when maxBatchSize is greater than CSV row length', { concurrency: true }, async () => {
     await createCSVMockStream([
       ['columnA', 'columnB'],
This page took 0.047824 seconds and 4 git commands to generate.