-
Notifications
You must be signed in to change notification settings - Fork 72
/
repeat.ts
36 lines (31 loc) · 929 Bytes
/
repeat.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
import { IterableX } from './iterablex';
export class RepeatIterable<TSource> extends IterableX<TSource> {
private _source: Iterable<TSource>;
private _count: number;
constructor(source: Iterable<TSource>, count: number) {
super();
this._source = source;
this._count = count;
}
*[Symbol.iterator]() {
if (this._count === -1) {
while (1) {
for (let item of this._source) {
yield item;
}
}
} else {
for (let i = 0; i < this._count; i++) {
for (let item of this._source) {
yield item;
}
}
}
}
}
export function repeat<TSource>(source: Iterable<TSource>, count: number = -1): IterableX<TSource> {
return new RepeatIterable<TSource>(source, count);
}
export function repeatStatic<TSource>(value: TSource, count: number = -1): IterableX<TSource> {
return new RepeatIterable<TSource>(IterableX.of(value), count);
}