-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
62 lines (51 loc) · 1.68 KB
/
index.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
54
55
56
57
58
59
60
61
62
export interface TrancioIterable<T> extends IterableIterator<T[]> {
[Symbol.iterator]: () => IterableIterator<T[]>
(): T[] | undefined
}
/**
* Lazily splits into groups the length of `size` an array. If `array` can't be split evenly, the final chunk will be the remaining elements.
*
* @param array The array to process.
* @param size The length of each chunk.
*
* @yields A chunk of `array` containing `size` elements. With each iteration the slice range increseas by `size`, starting with `[0, size)`, it continues to `[size, size + size)`, and so on.
* @returns A function that returns a chunk as described in `@yields`, when there are no more chunks it returns `undefined`.
*
* @example
* ```ts
* import { trancio } from 'trancio'
*
* const array = [...Array(5).keys()]
* const chunks = [...trancio(array, 3)]
*
* console.log(chunks)
* // => [ [0, 1, 2], [3, 4] ]
* ```
*/
export const trancio = <T>(array: T[], size: number): TrancioIterable<T> => {
const safeSize = size < 0 ? 0 : size
const tranci = Math.ceil(array.length / safeSize)
const generatorObject: IteratorResult<T[], void> = Object.create(null)
let trancioNumber = 0
const generator: TrancioIterable<T> = () => {
let chunk: T[] | undefined
if (trancioNumber < tranci) {
chunk = array.slice(
trancioNumber * safeSize,
trancioNumber * safeSize + safeSize
)
}
trancioNumber += 1
return chunk
}
generator.next = () => {
generatorObject.value = generator()
generatorObject.done = trancioNumber > tranci
return generatorObject
}
generator[Symbol.iterator] = (): IterableIterator<T[]> => ({
next: generator.next,
[Symbol.iterator]: generator[Symbol.iterator],
})
return generator
}