export default class StringIterator { #gen: Generator; #result: IteratorResult; constructor(compressedString: string) { this.#gen = RLEGenerator(compressedString); this.#result = this.#gen.next(); } next() { const value = this.#result.value; if (typeof value === "undefined") return ""; this.#result = this.#gen.next(); return value; } hasNext() { return !this.#result.done; } } export function* RLEGenerator( encoding: string, ): Generator { for (let i = 0; i < encoding.length; i += 2) { const count = Number(encoding[i + 1]); for (let j = 0; j < count; j++) { yield encoding[i]; } } }