iteragain
Version:
Javascript Iterable/Iterator/Generator-function utilities.
44 lines • 1.85 kB
JavaScript
/** An Iterator that yields windows or tuples of various sizes and offsets/intervals from the input `iterator`. */
var WindowsIterator = /** @class */ (function () {
function WindowsIterator(iterator, length, offset, fill) {
this.iterator = iterator;
this.length = length;
this.offset = offset;
this.fill = fill;
this.prev = [];
this.nextResult = { done: false, value: undefined };
/** The number of elements inbetween windows. */
this.unused = this.offset - this.length;
}
WindowsIterator.prototype[Symbol.iterator] = function () {
return this;
};
WindowsIterator.prototype.next = function () {
var _a, _b;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (this.nextResult.done)
return this.nextResult;
while (this.prev.length < this.length && !(this.nextResult = (_a = this.iterator).next.apply(_a, args)).done)
this.prev.push(this.nextResult.value);
if (this.prev.length < this.length) {
if (this.fill === undefined || !this.prev.length)
return this.nextResult;
for (var i = this.prev.length; i < this.length; i++)
this.prev.push(this.fill);
// this.prev.push(...Array.from({ length: this.length - this.prev.length }, _ => this.fill));
}
// Remove unused elements:
for (var i = 0; i < this.unused; i++)
(_b = this.iterator).next.apply(_b, args);
var value = this.prev.slice();
this.prev.splice(0, this.offset);
return { done: false, value: value };
};
return WindowsIterator;
}());
export { WindowsIterator };
export default WindowsIterator;
//# sourceMappingURL=WindowsIterator.js.map