import { IEnumerable } from "./interface/IEnumerable"; import IEnumerator from "./interface/IEnumerator"; class CollectionEnumerator implements IEnumerator{ private index = 0; constructor(private array: Array){ } next(): IEnumerator { this.index ++; return this; } get Current() { return { value : this.array[this.index], done : this.array.length > 0 ? this.index === this.array.length - 1 : true, }; } } export abstract class Collection implements IEnumerable{ getEnumerator(): IEnumerator { return new CollectionEnumerator(this.toArray()); } public toArray(){ const arr: Array = []; this.__iterate((item, index) => { arr[index] = item; }); return arr; } protected abstract __iterate(fn: (item: T, index: number) => void): void; }