{"version":3,"file":"index.esm.mjs","sources":["../src/index.ts"],"sourcesContent":["import { ITSRequireAtLeastOne } from 'ts-type/lib/type/record';\nimport { ITSValueOrArray } from 'ts-type';\n\n/**\n * 分塊陣列的型別定義\n * Type definition for chunked array\n *\n * 相當於 T[][]\n * Equivalent to T[][]\n */\nexport interface IChunkArray<T> extends Array<T[]>\n{\n\n}\n\n/**\n * 映射回調函式介面\n * Map callback function interface\n *\n * @template T - 元素類型 / Element type\n * @template R - 回傳類型 / Return type\n */\nexport interface IMapCallback<T, R = T>\n{\n\t(value: T[], index: number, array: IChunkArray<T>): R\n\n\t(value: T[], index: number, array: IChunkArray<T>): T\n}\n\n/**\n * 陣列分塊選項\n * Array chunking options\n *\n * @property inputArray - 來源陣列 / Source array\n * @property maxChunkLength - 每個區塊的最大長度 / Maximum length per chunk\n * @property maxChunkSize - 每個區塊的最大大小 / Maximum size per chunk\n */\nexport type IOptions<T> = {\n\n\t/**\n\t * source array\n\t */\n\tinputArray: T[],\n\n} & ITSRequireAtLeastOne<{\n\n\t/**\n\t * Split an array into arrays with max chunk length\n\t * 將陣列分割成最大長度的區塊\n\t */\n\tmaxChunkLength?: number,\n\t/**\n\t * Split an array into arrays of chunk with max size\n\t * 將陣列分割成最大大小的區塊\n\t */\n\tmaxChunkSize?: number,\n\n}, 'maxChunkLength' | 'maxChunkSize'>;\n\n/**\n * 對分塊後的陣列進行映射轉換\n * Map over chunked arrays with transformation\n *\n * 應用情境：\n * - 分塊處理大型資料集\n * - 批次 API 請求\n * - 將連續資料分割後進行平行處理\n * - Process large datasets in chunks\n * - Batch API requests\n * - Split continuous data for parallel processing\n *\n * @param options - 分塊選項 / Chunking options\n * @returns 映射後的結果陣列 / Mapped result array\n *\n * @example\n * // 預設返回每個區塊的第一個元素\n * arrayChunkMap({\n *   inputArray: [1, 2, 3, 4, 5, 6, 7, 8],\n *   maxChunkLength: 4\n * }); // => [1, 3, 5, 7]\n *\n * // 返回每個區塊的最後一個元素\n * arrayChunkMap({\n *   inputArray: [1, 2, 3, 4, 5, 6, 7, 8],\n *   maxChunkLength: 4,\n *   mapMethod: true\n * }); // => [2, 4, 6, 8]\n *\n * // 自訂映射函式\n * arrayChunkMap({\n *   inputArray: [1, 2, 3, 4, 5, 6],\n *   maxChunkSize: 2,\n *   mapMethod: (chunk) => chunk.reduce((a, b) => a + b, 0)\n * }); // => [3, 7, 11]\n */\nexport function arrayChunkMap<T, R = T>(options: IOptions<T> & {\n\tmapMethod: IMapCallback<T, R>\n}): R[]\n/**\n * by default will return a array with first value in chunk\n *\n * if mapMethod = true will return last value of chunk\n *\n * if give mapMethod is function\n * will calls a defined callback function on each element of an array, and returns an array that contains the results.\n */\nexport function arrayChunkMap<T>(options: IOptions<T> & {\n\tmapMethod: boolean\n}): T[]\n/**\n * by default will return a array with first value in chunk\n *\n * if mapMethod = true will return last value of chunk\n *\n * if give mapMethod is function\n * will calls a defined callback function on each element of an array, and returns an array that contains the results.\n */\nexport function arrayChunkMap<T, R = T>(options: IOptions<T> & {\n\tmapMethod?: boolean | IMapCallback<T, R>\n}): R[]\nexport function arrayChunkMap<T, R = T>(options: IOptions<T> & {\n\tmapMethod?: boolean | IMapCallback<T, R>\n}): R[]\n{\n\tconst { inputArray, maxChunkLength, maxChunkSize } = options;\n\tlet { mapMethod } = options;\n\n\tlet result: IChunkArray<T>;\n\n\tif (maxChunkLength != null)\n\t{\n\t\tresult = arrayChunkSplit(inputArray, maxChunkLength);\n\t}\n\telse if (maxChunkSize != null)\n\t{\n\t\tresult = arrayChunkBySize(inputArray, maxChunkSize);\n\t}\n\telse\n\t{\n\t\tthrow new TypeError(`maxChunkLength or maxChunkSize is required`)\n\t}\n\n\tif (typeof mapMethod !== 'function')\n\t{\n\t\tif (mapMethod)\n\t\t{\n\t\t\t// @ts-ignore\n\t\t\tmapMethod = (value) => value[value.length - 1];\n\t\t}\n\t\telse\n\t\t{\n\t\t\t// @ts-ignore\n\t\t\tmapMethod = (value) => value[0];\n\t\t}\n\t}\n\n\treturn result.map(mapMethod as any) as any as R[];\n}\n\n/**\n * 根據區塊大小分割陣列\n * Split array into chunks by size\n *\n * 應用情境：\n * - 固定大小的資料批次\n * - 每 N 個元素一組的處理\n * - Fixed-size data batches\n * - Process every N elements as a group\n *\n * @param arr - 要分割的陣列 / Array to split\n * @param maxChunkSize - 每個區塊的大小 / Size of each chunk\n * @returns 分塊後的陣列 / Chunked array\n *\n * @example\n * // 固定區塊大小\n * arrayChunkBySize([1, 2, 3, 4, 5, 6, 7, 8], 5); // => [[1, 2, 3, 4, 5], [6, 7, 8]]\n *\n * // 多個區塊大小\n * arrayChunkBySize([1, 2, 3, 4, 5], [2, 3]); // => [[1, 2], [3, 4, 5]]\n */\nexport function arrayChunkBySize<T>(arr: T[], maxChunkSize: ITSValueOrArray<number>): IChunkArray<T>\n{\n\tconst result: IChunkArray<T> = [];\n\t//let part: T[] = [];\n\n\tconst { length } = arr;\n\n\tif (Array.isArray(maxChunkSize))\n\t{\n\t\tif (!maxChunkSize.filter(v => v && v < length).length)\n\t\t{\n\t\t\tthrow new RangeError(`expected maxChunkSize.length > 0 and each values < ${length} but got ${maxChunkSize}`)\n\t\t}\n\n\t\tlet cur = 0;\n\t\tlet next: number;\n\n\t\tfor (let i of maxChunkSize)\n\t\t{\n\t\t\tnext = cur + i;\n\n\t\t\tresult.push(arr.slice(cur, next));\n\n\t\t\tif (next >= length)\n\t\t\t{\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcur = next;\n\t\t}\n\n\t\tif (next < length)\n\t\t{\n\t\t\tresult.push(arr.slice(cur));\n\t\t}\n\t}\n\telse if (typeof maxChunkSize !== 'number' || maxChunkSize < 1)\n\t{\n\t\tthrow new RangeError(`expected maxChunkSize > 0 but got ${maxChunkSize}`)\n\t}\n\telse\n\t{\n\t\tfor (let i = 0; i < length; i++)\n\t\t{\n\t\t\tlet next = i + maxChunkSize;\n\n\t\t\tresult.push(arr.slice(i, next));\n\n\t\t\ti = next - 1;\n\t\t}\n\t}\n\n\treturn result;\n}\n\n/**\n * 根據區塊數量分割陣列\n * Split array into specified number of chunks\n *\n * 應用情境：\n * - 均分資料到 N 個處理器\n * - 將資料均勻分配到多個頁面\n * - Distribute data evenly to N processors\n * - Distribute data evenly across multiple pages\n *\n * @param arr - 要分割的陣列 / Array to split\n * @param maxChunkLength - 區塊數量 / Number of chunks\n * @returns 分塊後的陣列 / Chunked array\n *\n * @example\n * // 分成 4 個區塊\n * arrayChunkSplit([1, 2, 3, 4, 5, 6, 7, 8], 4); // => [[1, 2], [3, 4], [5, 6], [7, 8]]\n *\n * // 分成 3 個區塊\n * arrayChunkSplit([1, 2, 3, 4, 5], 3); // => [[1, 2], [3, 4], [5]]\n */\nexport function arrayChunkSplit<T>(arr: T[], maxChunkLength: number)\n{\n\tif (typeof maxChunkLength !== 'number' || maxChunkLength < 1)\n\t{\n\t\tthrow new RangeError(`expected maxChunkLength > 0 but got ${maxChunkLength}`)\n\t}\n\n\tconst maxChunkSize = Math.max(Math.round(arr.length / maxChunkLength), 1);\n\treturn arrayChunkBySize(arr, maxChunkSize);\n}\n\nexport default arrayChunkSplit\n"],"names":["arrayChunkMap","options","inputArray","maxChunkLength","maxChunkSize","result","mapMethod","arrayChunkSplit","TypeError","arrayChunkBySize","value","length","map","arr","Array","isArray","filter","v","RangeError","next","cur","i","push","slice","Math","max","round"],"mappings":"AAwHM,SAAUA,cAAwBC;EAIvC,OAAMC,YAAEA,GAAUC,gBAAEA,GAAcC,cAAEA,KAAiBH;EACrD,IAEII,IAFAC,WAAEA,KAAcL;EAIpB,IAAsB,QAAlBE,GAEHE,IAASE,gBAAgBL,GAAYC,SAEjC;IAAA,IAAoB,QAAhBC,GAMR,MAAM,IAAII,UAAU;IAJpBH,IAASI,iBAAiBP,GAAYE;AAKvC;EAgBA,OAdyB,qBAAdE,MAKTA,IAHGA,IAGUI,KAAUA,EAAMA,EAAMC,SAAS,KAK/BD,KAAUA,EAAM,KAIxBL,EAAOO,IAAIN;AACnB;;A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