ai
Version:
AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.
28 lines (24 loc) • 815 B
text/typescript
import { InvalidArgumentError } from '../error/invalid-argument-error';
/**
* Splits an array into chunks of a specified size.
*
* @template T - The type of elements in the array.
* @param {T[]} array - The array to split.
* @param {number} chunkSize - The size of each chunk.
* @returns {T[][]} - A new array containing the chunks.
* @throws {InvalidArgumentError} If the chunk size is not greater than 0.
*/
export function splitArray<T>(array: T[], chunkSize: number): T[][] {
if (chunkSize <= 0) {
throw new InvalidArgumentError({
parameter: 'chunkSize',
value: chunkSize,
message: 'chunkSize must be greater than 0',
});
}
const result = [];
for (let i = 0; i < array.length; i += chunkSize) {
result.push(array.slice(i, i + chunkSize));
}
return result;
}