forked from remeda/remeda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chunk.ts
53 lines (49 loc) · 1.41 KB
/
chunk.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { purry } from './purry';
/**
* Split an array into groups the length of `size`. If `array` can't be split evenly, the final chunk will be the remaining elements.
* @param array the array
* @param size the length of the chunk
* @signature
* R.chunk(array, size)
* @example
* R.chunk(['a', 'b', 'c', 'd'], 2) // => [['a', 'b'], ['c', 'd']]
* R.chunk(['a', 'b', 'c', 'd'], 3) // => [['a', 'b', 'c'], ['d']]
* @data_first
* @category Array
*/
export function chunk<T>(
array: ReadonlyArray<T>,
size: number
): Array<Array<T>>;
/**
* Split an array into groups the length of `size`. If `array` can't be split evenly, the final chunk will be the remaining elements.
* @param size the length of the chunk
* @signature
* R.chunk(size)(array)
* @example
* R.chunk(2)(['a', 'b', 'c', 'd']) // => [['a', 'b'], ['c', 'd']]
* R.chunk(3)(['a', 'b', 'c', 'd']) // => [['a', 'b', 'c'], ['d']]
* @data_last
* @category Array
*/
export function chunk<T>(
size: number
): (array: ReadonlyArray<T>) => Array<Array<T>>;
export function chunk() {
return purry(_chunk, arguments);
}
function _chunk<T>(array: Array<T>, size: number) {
const ret: Array<Array<T>> = [];
let current: Array<T> | null = null;
array.forEach(x => {
if (!current) {
current = [];
ret.push(current);
}
current.push(x);
if (current.length === size) {
current = null;
}
});
return ret;
}