Binato/server/objects/FunkyArray.ts
2022-11-16 15:25:46 +00:00

74 lines
No EOL
1.6 KiB
TypeScript

export class FunkyArray<T> {
private items:any = {};
private itemKeys:Array<string> = Object.keys(this.items);
private iterableArray:Array<T> = new Array<T>();
public add(uuid:string, item:T, regenerate:boolean = true) : T {
this.items[uuid] = item;
if (regenerate) {
this.itemKeys = Object.keys(this.items);
this.regenerateIterableArray();
}
return this.items[uuid];
}
public remove(uuid:string, regenerate:boolean = true) {
delete this.items[uuid];
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 (let 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<string> {
return this.itemKeys;
}
public getItems() : any {
return this.items;
}
public getIterableItems() : Array<T> {
return this.iterableArray;
}
}