UNPKG

iteragain

Version:

Javascript Iterable/Iterator/Generator-function utilities.

39 lines 1.34 kB
import toArray from '../toArray'; /** Returns the cartesian product of the input `iterators`. */ export class ProductIterator { constructor(iterators, repeat) { this.done = false; this.pools = []; this.i = 0; const iterated = iterators.map(toArray); 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 }; } } export default ProductIterator; //# sourceMappingURL=ProductIterator.js.map