From dda06a0873636b74f75c06bc36900937dd27abab Mon Sep 17 00:00:00 2001 From: FoxxMD Date: Fri, 27 Mar 2026 16:08:47 +0000 Subject: [PATCH] add chunk array function --- src/core/DataUtils.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/core/DataUtils.ts b/src/core/DataUtils.ts index 873139f1..66ab88e6 100644 --- a/src/core/DataUtils.ts +++ b/src/core/DataUtils.ts @@ -7,6 +7,7 @@ import { diffDelta, applyDelta, IJsonDelta, DeltaOptions } from 'json-diff-ts'; import chalk from 'chalk'; import clone from "clone"; import ConsoleFormatter from "jsondiffpatch/formatters/console"; +import assert from "node:assert"; const console = new ConsoleFormatter(); @@ -85,3 +86,16 @@ export const generateArray = (size: number, gen: (index: number) => any) => { return Array.from(Array(size), (v,k) => gen(k)); } +/** Return an array in chunks + * + * https://stackoverflow.com/a/8495740/1469797 + */ +export const chunkArray = (chunkSize: number, arr: T[]): T[][] => { + assert(chunkSize !== 0, 'chunkSize cannot be 0'); + const chunks: T[][] = []; + for (let i = 0; i < arr.length; i += chunkSize) { + const chunk = arr.slice(i, i + chunkSize); + chunks.push(chunk); + } + return chunks; +} \ No newline at end of file -- 2.51.2