74 lines
No EOL
1.6 KiB
TypeScript
74 lines
No EOL
1.6 KiB
TypeScript
export default class FunkyArray<T> {
|
|
private items:{ [id: string]: T } = {};
|
|
private itemKeys:Array<string> = Object.keys(this.items);
|
|
private iterableArray:Array<T> = new Array<T>();
|
|
|
|
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<T>();
|
|
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<string> {
|
|
return this.itemKeys;
|
|
}
|
|
|
|
public getItems() : { [id: string]: T } {
|
|
return this.items;
|
|
}
|
|
|
|
public getIterableItems() : Array<T> {
|
|
return this.iterableArray;
|
|
}
|
|
} |