UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

28 lines 910 B
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. */ export class CycleIterator { constructor(iterator, times) { this.iterator = iterator; this.times = times; this.values = []; } [Symbol.iterator]() { return this; } next(...args) { const next = this.iterator.next(...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(...args); } this.values.push(next.value); return next; } } export default CycleIterator; //# sourceMappingURL=CycleIterator.js.map