lik/ts/lik.objectmap.ts

133 lines
2.6 KiB
TypeScript
Raw Normal View History

import * as plugins from "./lik.plugins";
2016-07-31 12:36:28 +00:00
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> {
private objectArray: T[] = [];
2017-06-17 12:44:55 +00:00
/**
* returns a new instance
*/
constructor() {
2017-06-17 12:44:55 +00:00
// nothing here
}
/**
* 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
*/
add(objectArg: T): boolean {
2017-07-05 12:29:08 +00:00
if (this.checkForObject(objectArg)) {
// the object is already in the objectmap
return false;
2017-07-05 12:29:08 +00:00
} else {
// the object is not yet in the objectmap
this.objectArray.push(objectArg);
return true;
2017-07-05 12:29:08 +00:00
}
2017-06-17 12:44:55 +00:00
}
/**
* like .add but adds an whole array of objects
*/
addArray(objectArrayArg: T[]) {
2017-06-17 12:44:55 +00:00
for (let item of objectArrayArg) {
this.add(item);
}
2017-06-17 12:44:55 +00:00
}
/**
* check if object is in Objectmap
*/
checkForObject(objectArg: T) {
return this.objectArray.indexOf(objectArg) !== -1;
2017-06-17 12:44:55 +00:00
}
/**
* find object
*/
find(findFunction: IObjectmapFindFunction<T>) {
let resultArray = this.objectArray.filter(findFunction);
2017-06-17 12:44:55 +00:00
if (resultArray.length > 0) {
return resultArray[0];
2017-06-17 12:44:55 +00:00
} else {
return null;
}
2017-06-17 12:44:55 +00:00
}
/**
* finds a specific element and then removes it
*/
findOneAndRemove(findFunction: IObjectmapFindFunction<T>): T {
let 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
*/
async forEach(functionArg: IObjectmapForEachFunction<T>) {
for (let object of this.objectArray) {
await functionArg(object);
}
2017-06-17 12:44:55 +00:00
}
/**
* gets an object in the Observablemap and removes it, so it can't be retrieved again
*/
getOneAndRemove(): T {
return this.objectArray.shift();
2017-06-17 12:44:55 +00:00
}
/**
* returns a cloned array of all the objects currently in the Objectmap
*/
getArray() {
return plugins.lodash.cloneDeep(this.objectArray);
2017-06-17 12:44:55 +00:00
}
/**
* check if Objectmap ist empty
*/
isEmpty(): boolean {
2017-06-17 12:44:55 +00:00
if (this.objectArray.length === 0) {
return true;
2017-06-17 12:44:55 +00:00
} else {
return false;
2016-09-29 20:05:20 +00:00
}
2017-06-17 12:44:55 +00:00
}
/**
* remove object from Objectmap
*/
remove(objectArg: T) {
let replacementArray = [];
2017-06-17 12:44:55 +00:00
for (let item of this.objectArray) {
if (item !== objectArg) {
replacementArray.push(item);
2017-06-17 12:44:55 +00:00
}
}
this.objectArray = replacementArray;
2017-06-17 12:44:55 +00:00
}
/**
* wipe Objectmap
*/
wipe() {
this.objectArray = [];
2017-06-17 12:44:55 +00:00
}
}