export default class FunkyArray { private items:{ [id: string]: T } = {}; private itemKeys:Array = Object.keys(this.items); private iterableArray:Array = new Array(); public add(key:string, item:T, regenerate:boolean = true) : T { this.items[key] = item; if (regenerate) { this.itemKeys = Object.keys(this.items); this.regenerateIterableArray(); } return this.items[key]; } public remove(key:string, regenerate:boolean = true) { delete this.items[key]; if (regenerate) { this.itemKeys = Object.keys(this.items); this.regenerateIterableArray(); } } public removeFirstItem(regenerate:boolean = true) : void { delete this.items[this.itemKeys[0]]; this.itemKeys = Object.keys(this.items); if (regenerate) this.regenerateIterableArray(); } public regenerateIterableArray() : void { this.iterableArray = new Array(); for (const itemKey of this.itemKeys) { this.iterableArray.push(this.items[itemKey]); } this.itemKeys = Object.keys(this.items); } public getFirstItem() : T { return this.items[this.itemKeys[0]]; } public getLength() : number { return this.itemKeys.length; } public getKeyById(id:number) : string { return this.itemKeys[id]; } public getById(id:number) : T | undefined { return this.items[this.itemKeys[id]]; } public getByKey(key:string) : T | undefined { if (key in this.items) { return this.items[key]; } return undefined; } public getKeys() : Array { return this.itemKeys; } public getItems() : { [id: string]: T } { return this.items; } public getIterableItems() : Array { return this.iterableArray; } }