/
iterable.ts
55 lines (48 loc) · 1.33 KB
/
iterable.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
import type { IGen } from "../api";
/**
* Wraps given ES6 iterable to provide full {@link IGen} implementation.
*
* @remarks
* Calling `.next()` on this wrapped instance will always succeed, even if the
* original iterable already is exhausted (in which case the last valid value
* will be repeated ad infinitum).
*
* The `initial` value is required to satisfy `.deref()` and the case where the
* iterable doesn't provide a single value.
*
* @param src
* @param initial
*/
export const iterable = <T>(src: Iterable<T>, initial: T) =>
new $Iterable<T>(src, initial);
export class $Iterable<T> implements IGen<T> {
protected _iter: Iterator<T> | null;
protected _val: T;
constructor(src: Iterable<T>, initial: T) {
this._iter = src[Symbol.iterator]();
this._val = initial;
}
deref() {
return this._val;
}
*[Symbol.iterator]() {
while (true) yield this.next();
}
next() {
if (this._iter) {
const res = this._iter.next();
if (!res.done) {
this._val = res.value;
} else {
this._iter = null;
}
}
return this._val;
}
take(num: number, out: T[] = [], idx = 0) {
for (; --num >= 0; ) {
out[idx++] = this.next();
}
return out;
}
}