iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
35 lines • 1.23 kB
JavaScript
import toIterator from '../toIterator';
/**
* Cycles through the input `iterator`'s values a certain number of `times`. So when the input iterator is done, the
* next iterator result will cycle back to the first value of the input iterator.
*/
var CycleIterator = /** @class */ (function () {
function CycleIterator(iterator, times) {
this.iterator = iterator;
this.times = times;
this.values = [];
}
CycleIterator.prototype[Symbol.iterator] = function () {
return this;
};
CycleIterator.prototype.next = function () {
var _a;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var next = (_a = this.iterator).next.apply(_a, args);
if (next.done) {
if (!this.values.length || this.times-- <= 0)
return next;
this.iterator = toIterator(this.values.splice(0, this.values.length));
return this.next.apply(this, args);
}
this.values.push(next.value);
return next;
};
return CycleIterator;
}());
export { CycleIterator };
export default CycleIterator;
//# sourceMappingURL=CycleIterator.js.map