UNPKG

depends-txt

Version:
31 lines 747 B
export function peekable(it) { return new PeekableIterator(it); } export class PeekableIterator { it; #peeked = []; #done = false; constructor(it) { this.it = it; } [Symbol.iterator]() { return this; } next() { return this.#peeked.shift() ?? this.#next(); } peekNth(n) { while (!this.#done && this.#peeked.length <= n) { this.#peeked.push(this.#next()); } return this.#peeked[n] ?? this.#next(); } #next() { const result = this.#done ? { value: undefined, done: this.#done } : this.it.next(); this.#done = result.done ?? false; return result; } } //# sourceMappingURL=peekable.js.map