// src/ArrayIterator.ts import {uniqueId} from "lui-g"; /** * Takes a nested array of T items and iterates over it in normal or reverse order * (without changing the original array or creating a reversed one). * Supports nested (N dimensional) arrays, which can be iterated recursively. */ export default class ArrayIterator { // this signature would be better but then the iterator methods return value won't work out // export default class ArrayIterator implements ReadonlyArray { readonly uniqueId: string = uniqueId(); protected readonly iterated: ReadonlyArray; protected readonly isReverse: boolean = false; [index: number]: T; // Add this index signature constructor(iterated: ReadonlyArray, reverse: boolean = false) { this.iterated = iterated; this.isReverse = reverse; return new Proxy(this, { get: (target: ArrayIterator, prop: string | symbol) => { // array access by numeric index if (typeof prop === "string") { const index = Number(prop); if (!isNaN(index)) { return target.iterated[target.isReverse ? target.iterated.length - 1 - index : index]; } } return Reflect.get(target, prop); } }); } get length() { return this.iterated?.length; } *[Symbol.iterator](): Generator> { const keys = [...Array(this.iterated.length).keys()]; const indices = this.isReverse ? keys.reverse() : keys; for (const i of indices) { const ret = this.iterated[i]; if (Array.isArray(ret)) { yield new ArrayIterator(ret); } else { yield ret; } } } reverse(): ArrayIterator { return new ArrayIterator(this.iterated, !this.isReverse); } }