generaltranslation
Version:
A language toolkit for AI developers
53 lines (52 loc) • 1.75 kB
TypeScript
/**
* Splits an array into batches of a specified size.
* @param items - The array to split into batches
* @param batchSize - The maximum size of each batch
* @returns An array of batches
*/
export declare function createBatches<T>(items: T[], batchSize: number): T[][];
/**
* Result of processing batches
*/
export interface BatchList<T> {
/** The items successfully processed across all batches */
data: T[];
/** The total number of items processed */
count: number;
/** The number of batches processed */
batchCount: number;
}
/**
* Options for batch processing
*/
export interface BatchProcessOptions {
/** Maximum number of items per batch (default: 100) */
batchSize?: number;
/** Whether to process batches in parallel (default: true) */
parallel?: boolean;
}
/**
* Processes items in batches using a provided processor function.
*
* @param items - The items to process
* @param processor - Async function that processes a single batch and returns items
* @param options - Optional configuration for batch processing
* @returns Promise that resolves to a BatchList containing all processed items
*
* @example
* ```typescript
* const result = await processBatches(
* files,
* async (batch) => {
* const response = await uploadFiles(batch);
* return response.uploadedFiles;
* },
* { batchSize: 100 }
* );
*
* console.log(result.data); // All items
* console.log(result.count); // Total count
* console.log(result.batchCount); // Number of batches processed
* ```
*/
export declare function processBatches<TInput, TOutput>(items: TInput[], processor: (batch: TInput[]) => Promise<TOutput[]>, options?: BatchProcessOptions): Promise<BatchList<TOutput>>;