UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

44 lines 1.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WindowsIterator = void 0; /** An Iterator that yields windows or tuples of various sizes and offsets/intervals from the input `iterator`. */ class WindowsIterator { constructor(iterator, length, offset, fill) { this.iterator = iterator; this.length = length; this.offset = offset; this.fill = fill; this.prev = []; this.nextResult = { done: false, value: undefined }; if (length <= 0) throw new RangeError('length must be greater than 0'); if (offset <= 0) throw new RangeError('offset must be greater than 0'); this.unused = this.offset - this.length; } [Symbol.iterator]() { return this; } next(...args) { if (this.nextResult.done) return this.nextResult; while (this.prev.length < this.length && !(this.nextResult = this.iterator.next(...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 (let 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 (let i = 0; i < this.unused; i++) this.iterator.next(...args); const value = this.prev.slice(); this.prev.splice(0, this.offset); return { done: false, value }; } } exports.WindowsIterator = WindowsIterator; exports.default = WindowsIterator; //# sourceMappingURL=WindowsIterator.js.map