From 17a0f3c2821b412dd2562b8634286672fd0970ee Mon Sep 17 00:00:00 2001 From: Ardis Lu Date: Wed, 28 May 2025 00:26:30 -0700 Subject: [PATCH] Update `maxBatchSize` behavior to match type documentation **Any** value should start batching, instead of any value > 1. --- src/core.js | 10 ++++------ test/core.test.js | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/core.js b/src/core.js index 596cd59..9749f6e 100644 --- a/src/core.js +++ b/src/core.js @@ -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>`. + * (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>`. * @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)); diff --git a/test/core.test.js b/test/core.test.js index 5f6dc8f..1fe7f5b 100644 --- a/test/core.test.js +++ b/test/core.test.js @@ -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'], -- 2.47.3