-
Notifications
You must be signed in to change notification settings - Fork 0
/
splitter.ts
35 lines (29 loc) · 1.06 KB
/
splitter.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* @param array [Numbers array that contains the data you want to split]
* @param size [The maximum number of elements in each array]
* @returns [[...], [...]]
*/
export function chunkAverage(array: number[], size: number): number[] {
let renderedChunk: number[];
let chunkSum = 0;
const chunkArray = [...array];
const mainChunk: number[][] = [];
const averageChunks: number[] = [];
const backupChunks: number[][] = [];
const splitIndex = !Number.isNaN(size) ? size : 7;
while (chunkArray.length > 0) {
renderedChunk = chunkArray.splice(0, splitIndex);
if (renderedChunk.length === splitIndex) mainChunk.push(renderedChunk);
else backupChunks.push(renderedChunk);
}
for (let j = 0; j < mainChunk.length || j < backupChunks.length; j++) {
if (mainChunk.length) {
chunkSum = mainChunk[j].reduce((partialSum, a) => partialSum + a, 0);
averageChunks.push(chunkSum);
} else {
chunkSum = backupChunks[j].reduce((partialSum, a) => partialSum + a, 0);
averageChunks.push(chunkSum);
}
}
return averageChunks;
}