feat: initial commit
This commit is contained in:
39
src/functions/splitIntoChuks.ts
Normal file
39
src/functions/splitIntoChuks.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
Copyright 2024 Resultium LLC
|
||||
|
||||
This file is part of @resultium/utils.
|
||||
|
||||
@resultium/utils is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
any later version.
|
||||
|
||||
@resultium/utils is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with RCZ. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Splits an array into chunks
|
||||
*
|
||||
* @param array - array which shall be split into chunks
|
||||
* @param chunkSize - size of a single chunk
|
||||
* @returns array of chunks
|
||||
*/
|
||||
export const SplitIntoChunks = <T>(array: T[], chunkSize: number): T[][] => {
|
||||
return array.reduce<T[][]>((splitArray, item, index) => {
|
||||
const chunkIndex = Math.floor(index / chunkSize);
|
||||
|
||||
if (!splitArray[chunkIndex]) {
|
||||
splitArray[chunkIndex] = [];
|
||||
}
|
||||
|
||||
splitArray[chunkIndex]?.push(item);
|
||||
|
||||
return splitArray;
|
||||
}, []);
|
||||
};
|
||||
Reference in New Issue
Block a user