UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

30 lines 1.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CycleIterator = void 0; const toIterator_1 = require("../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. */ 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 && this.times-- > 0) { this.iterator = (0, toIterator_1.default)(this.values.splice(0, this.values.length)); return this.next(...args); } this.values.push(next.value); return next; } } exports.CycleIterator = CycleIterator; exports.default = CycleIterator; //# sourceMappingURL=CycleIterator.js.map