lik/ts/lik.objectmap.ts

220 lines
5.1 KiB
TypeScript
Raw Normal View History

2018-07-15 14:04:27 +00:00
import * as plugins from './lik.plugins';
2020-02-06 11:11:16 +00:00
import { FastMap } from './lik.fastmap';
2016-07-31 12:36:28 +00:00
2020-05-27 23:50:07 +00:00
export const uni = (prefix: string = 'uni') => {
2020-07-12 00:44:50 +00:00
return `${prefix}xxxxxxxxxxx`.replace(/[xy]/g, (c) => {
2020-05-27 23:50:07 +00:00
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};
export interface IObjectmapForEachFunction<T> {
(itemArg: T): void;
}
export interface IObjectmapFindFunction<T> {
(itemArg: T): boolean;
}
2016-07-31 12:36:28 +00:00
2016-07-30 22:54:46 +00:00
/**
* allows keeping track of objects
*/
export class ObjectMap<T> {
2020-02-06 11:11:16 +00:00
private fastMap = new FastMap<T>();
2017-06-17 12:44:55 +00:00
2019-11-27 23:15:13 +00:00
// events
public eventSubject = new plugins.smartrx.rxjs.Subject<any>();
2017-06-17 12:44:55 +00:00
/**
* returns a new instance
*/
constructor() {
2017-06-17 12:44:55 +00:00
// nothing here
}
2020-02-06 11:11:16 +00:00
/**
* adds an object mapped to a string
* the string must be unique
*/
addMappedUnique(uniqueKeyArg: string, objectArg: T) {
this.fastMap.addToMap(uniqueKeyArg, objectArg);
2020-02-06 11:11:16 +00:00
}
/**
* fastest way to get an object from the map
* @param uniqueKey
*/
public getMappedUnique(uniqueKeyArg: string) {
return this.fastMap.getByKey(uniqueKeyArg);
}
2020-02-06 11:11:16 +00:00
/**
* remove key
* @param functionArg
*/
public removeMappedUnique(uniqueKey: string) {
const object = this.getMappedUnique(uniqueKey);
}
2020-02-06 11:11:16 +00:00
2020-11-24 18:47:45 +00:00
public addSubject = new plugins.smartrx.rxjs.Subject<T>();
2017-06-17 12:44:55 +00:00
/**
* add object to Objectmap
2017-07-05 12:29:08 +00:00
* returns false if the object is already in the map
* returns true if the object was added successfully
2017-06-17 12:44:55 +00:00
*/
public add(objectArg: T): string {
// lets search for an existing unique key
for (const keyArg of this.fastMap.getKeys()) {
const object = this.fastMap.getByKey(keyArg);
if (object === objectArg) {
return keyArg;
}
2017-07-05 12:29:08 +00:00
}
// otherwise lets create it
2020-05-27 23:50:07 +00:00
const uniqueKey = uni('key');
this.addMappedUnique(uniqueKey, objectArg);
2020-11-24 18:47:45 +00:00
this.addSubject.next(objectArg);
return uniqueKey;
2017-06-17 12:44:55 +00:00
}
/**
* like .add but adds an whole array of objects
*/
2018-11-23 19:33:44 +00:00
public addArray(objectArrayArg: T[]) {
for (const item of objectArrayArg) {
this.add(item);
}
2017-06-17 12:44:55 +00:00
}
/**
* check if object is in Objectmap
*/
public checkForObject(objectArg: T): boolean {
return !!this.getKeyForObject(objectArg);
}
/**
* get key for object
2020-02-07 16:32:58 +00:00
* @param findFunction
*/
public getKeyForObject(objectArg: T) {
let foundKey: string = null;
for (const keyArg of this.fastMap.getKeys()) {
if (!foundKey && this.fastMap.getByKey(keyArg) === objectArg) {
foundKey = keyArg;
} else {
continue;
}
}
return foundKey;
2017-06-17 12:44:55 +00:00
}
/**
* find object
*/
public find(findFunction: IObjectmapFindFunction<T>): T {
for (const keyArg of this.fastMap.getKeys()) {
if (findFunction(this.fastMap.getByKey(keyArg))) {
return this.getMappedUnique(keyArg);
}
}
2017-06-17 12:44:55 +00:00
}
/**
* finds a specific element and then removes it
*/
2018-11-23 19:33:44 +00:00
public findOneAndRemove(findFunction: IObjectmapFindFunction<T>): T {
const foundElement = this.find(findFunction);
2017-06-17 12:44:55 +00:00
if (foundElement) {
this.remove(foundElement);
2016-07-30 22:54:46 +00:00
}
return foundElement;
2017-06-17 12:44:55 +00:00
}
/**
* run function for each item in Objectmap
*/
2018-11-23 19:33:44 +00:00
public async forEach(functionArg: IObjectmapForEachFunction<T>) {
for (const keyArg of this.fastMap.getKeys()) {
await functionArg(this.fastMap.getByKey(keyArg));
}
2017-06-17 12:44:55 +00:00
}
/**
* gets an object in the Observablemap and removes it, so it can't be retrieved again
*/
2018-11-23 19:33:44 +00:00
public getOneAndRemove(): T {
const keys = this.fastMap.getKeys();
2020-02-07 16:32:58 +00:00
if (keys.length === 0) {
return null;
} else {
const keyToUse = keys[0];
const removedItem = this.fastMap.removeFromMap(keyToUse);
this.eventSubject.next('remove');
return removedItem;
}
2017-06-17 12:44:55 +00:00
}
/**
* returns a cloned array of all the objects currently in the Objectmap
*/
public getArray(): T[] {
2018-11-23 19:33:44 +00:00
const returnArray: any[] = [];
for (const keyArg of this.fastMap.getKeys()) {
returnArray.push(this.fastMap.getByKey(keyArg));
2018-11-23 19:33:44 +00:00
}
return returnArray;
2017-06-17 12:44:55 +00:00
}
/**
* check if Objectmap ist empty
*/
2018-11-23 19:33:44 +00:00
public isEmpty(): boolean {
return this.fastMap.getKeys().length === 0;
2017-06-17 12:44:55 +00:00
}
/**
* remove object from Objectmap
*/
public remove(objectArg: T): T {
if (this.checkForObject(objectArg)) {
const keyArg = this.getKeyForObject(objectArg);
const removedObject = this.fastMap.removeFromMap(keyArg);
this.eventSubject.next('remove');
return removedObject;
}
return null;
2017-06-17 12:44:55 +00:00
}
/**
* wipe Objectmap
*/
2018-11-23 19:33:44 +00:00
public wipe() {
for (const keyArg of this.fastMap.getKeys()) {
this.fastMap.removeFromMap(keyArg);
}
2017-06-17 12:44:55 +00:00
}
/**
2020-05-25 13:18:53 +00:00
* returns a new Objectmap that includes
*/
public concat(objectMapArg: ObjectMap<T>) {
const concattedObjectMap = new ObjectMap<T>();
concattedObjectMap.fastMap.addAllFromOther(this.fastMap);
concattedObjectMap.fastMap.addAllFromOther(objectMapArg.fastMap);
return concattedObjectMap;
}
/**
* tries to merge another Objectmap
* Note: uniqueKeyCollisions will cause overwrite
* @param objectMapArg
*/
public addAllFromOther(objectMapArg: ObjectMap<T>) {
this.fastMap.addAllFromOther(objectMapArg.fastMap);
}
}