UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

43 lines 1.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ProductIterator = void 0; const toArray_1 = require("../toArray"); /** Returns the cartesian product of the input `iterators`. */ class ProductIterator { constructor(iterators, repeat) { this.done = false; this.pools = []; this.i = 0; const iterated = iterators.map(toArray_1.default); for (let i = 0; i < repeat; i++) this.pools.push(...iterated); this.indices = new Array(this.pools.length).fill(0); } [Symbol.iterator]() { return this; } next() { if (this.done) return { done: true, value: undefined }; const value = this.indices.map((v, i) => this.pools[i][v]); for (this.i = 0; this.i < this.indices.length; this.i++) { if (this.indices[this.pools.length - this.i - 1] < this.pools[this.pools.length - this.i - 1].length - 1) { this.indices[this.pools.length - this.i - 1]++; break; } } if (this.i === this.indices.length) { this.done = true; return { done: false, value }; } this.i--; while (this.i >= 0) { this.indices[this.pools.length - this.i - 1] = 0; this.i--; } return { done: false, value }; } } exports.ProductIterator = ProductIterator; exports.default = ProductIterator; //# sourceMappingURL=ProductIterator.js.map