-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
34 lines (24 loc) · 809 Bytes
/
Copy pathindex.js
File metadata and controls
34 lines (24 loc) · 809 Bytes
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
const chunkify = (array, chunks) => {
const result = [];
const averagePerChunk = array.length / chunks;
const itemsPerChunk = -Math.round(-averagePerChunk); // round half down
const tempArray = [...array];
let count = chunks > array.length ? array.length : chunks;
while (count) {
let chunk = [];
if (count === 1) {
chunk = tempArray.splice(0, tempArray.length);
} else {
chunk = tempArray.splice(0, itemsPerChunk);
}
result.push(chunk);
count -= 1;
}
return result;
};
module.exports = (input = [], arraySize = 1) => {
if (typeof arraySize !== 'number' || arraySize < 1) {
throw new TypeError('Array size must be a positive number');
}
return chunkify(input, arraySize);
};