File size: 763 Bytes
ebac87f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**
 * Chunk array into arrays of length at most `chunkSize`
 *
 * @param chunkSize must be greater than or equal to 1
 */
export function chunk<T extends unknown[] | string>(arr: T, chunkSize: number): T[] {
	if (isNaN(chunkSize) || chunkSize < 1) {
		throw new RangeError("Invalid chunk size: " + chunkSize);
	}

	if (!arr.length) {
		return [];
	}

	/// Small optimization to not chunk buffers unless needed
	if (arr.length <= chunkSize) {
		return [arr];
	}

	return range(Math.ceil(arr.length / chunkSize)).map((i) => {
		return arr.slice(i * chunkSize, (i + 1) * chunkSize);
	}) as T[];
}

function range(n: number, b?: number): number[] {
	return b
		? Array(b - n)
				.fill(0)
				.map((_, i) => n + i)
		: Array(n)
				.fill(0)
				.map((_, i) => i);
}