feat(collections): add new collection APIs, iterator support, and tree serialization utilities
This commit is contained in:
@@ -9,10 +9,18 @@ import * as plugins from './classes.plugins.js';
|
||||
* fast map allows for very quick lookups of objects with a unique key
|
||||
*/
|
||||
export class FastMap<T> {
|
||||
private mapObject: { [key: string]: T } = {};
|
||||
private mapObject = new Map<string, T>();
|
||||
|
||||
public isUniqueKey(keyArg: string): boolean {
|
||||
return this.mapObject[keyArg] ? false : true;
|
||||
return !this.mapObject.has(keyArg);
|
||||
}
|
||||
|
||||
public has(keyArg: string): boolean {
|
||||
return this.mapObject.has(keyArg);
|
||||
}
|
||||
|
||||
public get size(): number {
|
||||
return this.mapObject.size;
|
||||
}
|
||||
|
||||
public addToMap(
|
||||
@@ -23,35 +31,37 @@ export class FastMap<T> {
|
||||
}
|
||||
): boolean {
|
||||
if (this.isUniqueKey(keyArg) || (optionsArg && optionsArg.force)) {
|
||||
this.mapObject[keyArg] = objectArg;
|
||||
this.mapObject.set(keyArg, objectArg);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public getByKey(keyArg: string) {
|
||||
return this.mapObject[keyArg];
|
||||
public getByKey(keyArg: string): T | undefined {
|
||||
return this.mapObject.get(keyArg);
|
||||
}
|
||||
|
||||
public removeFromMap(keyArg: string): T {
|
||||
const removedItem = this.getByKey(keyArg);
|
||||
delete this.mapObject[keyArg];
|
||||
const removedItem = this.mapObject.get(keyArg);
|
||||
this.mapObject.delete(keyArg);
|
||||
return removedItem;
|
||||
}
|
||||
|
||||
public getKeys() {
|
||||
const keys: string[] = [];
|
||||
for (const keyArg in this.mapObject) {
|
||||
if (this.mapObject[keyArg]) {
|
||||
keys.push(keyArg);
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
public getKeys(): string[] {
|
||||
return Array.from(this.mapObject.keys());
|
||||
}
|
||||
|
||||
public values(): T[] {
|
||||
return Array.from(this.mapObject.values());
|
||||
}
|
||||
|
||||
public entries(): [string, T][] {
|
||||
return Array.from(this.mapObject.entries());
|
||||
}
|
||||
|
||||
public clean() {
|
||||
this.mapObject = {};
|
||||
this.mapObject.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -94,4 +104,8 @@ export class FastMap<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public [Symbol.iterator](): Iterator<[string, T]> {
|
||||
return this.mapObject.entries();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user