2016-09-21 11:44:11 +00:00
|
|
|
import * as plugins from './lik.plugins'
|
2016-07-31 12:36:28 +00:00
|
|
|
|
2016-08-08 15:29:44 +00:00
|
|
|
export interface IObjectmapForEachFunction<T> {
|
|
|
|
(itemArg: T): void
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
2016-08-08 14:00:14 +00:00
|
|
|
|
2016-08-08 15:29:44 +00:00
|
|
|
export interface IObjectmapFindFunction<T> {
|
|
|
|
(itemArg: T): boolean
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
2016-07-31 12:36:28 +00:00
|
|
|
|
2016-07-30 22:54:46 +00:00
|
|
|
/**
|
|
|
|
* allows keeping track of objects
|
|
|
|
*/
|
2016-08-08 15:29:44 +00:00
|
|
|
export class Objectmap<T> {
|
2016-09-21 11:44:11 +00:00
|
|
|
private objectArray: T[] = []
|
2016-08-07 16:07:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a new instance
|
|
|
|
*/
|
2016-08-08 15:29:44 +00:00
|
|
|
constructor() {
|
2016-08-07 16:07:21 +00:00
|
|
|
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
2016-08-07 16:07:21 +00:00
|
|
|
|
2016-07-31 12:36:28 +00:00
|
|
|
/**
|
|
|
|
* add object to Objectmap
|
|
|
|
*/
|
2016-09-21 11:44:11 +00:00
|
|
|
add(objectArg: T) {
|
|
|
|
this.objectArray.push(objectArg)
|
|
|
|
}
|
2016-07-31 12:36:28 +00:00
|
|
|
|
2016-09-22 10:00:33 +00:00
|
|
|
/**
|
|
|
|
* like .add but adds an whole array of objects
|
|
|
|
*/
|
|
|
|
addArray(objectArrayArg: T[]) {
|
|
|
|
for (let item of objectArrayArg) {
|
|
|
|
this.add(item)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-31 12:36:28 +00:00
|
|
|
/**
|
|
|
|
* remove object from Objectmap
|
|
|
|
*/
|
2016-09-21 11:44:11 +00:00
|
|
|
remove(objectArg: T) {
|
|
|
|
let replacmentArray = []
|
2016-08-08 15:29:44 +00:00
|
|
|
for (let item of this.objectArray) {
|
|
|
|
if (item !== objectArg) {
|
2016-09-21 11:44:11 +00:00
|
|
|
replacmentArray.push(item)
|
2016-07-30 22:54:46 +00:00
|
|
|
}
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
|
|
|
this.objectArray = replacmentArray
|
|
|
|
}
|
2016-07-31 12:36:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* check if object is in Objectmap
|
|
|
|
*/
|
2016-09-21 11:44:11 +00:00
|
|
|
checkForObject(objectArg: T) {
|
2016-08-08 15:29:44 +00:00
|
|
|
return this.objectArray.indexOf(objectArg) !== -1
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
2016-08-08 14:00:14 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* find object
|
|
|
|
*/
|
2016-08-08 15:29:44 +00:00
|
|
|
find(findFunction: IObjectmapFindFunction<T>) {
|
2016-09-21 11:44:11 +00:00
|
|
|
let resultArray = this.objectArray.filter(findFunction)
|
2016-08-08 15:29:44 +00:00
|
|
|
if (resultArray.length > 0) {
|
2016-09-21 11:44:11 +00:00
|
|
|
return resultArray[0]
|
2016-08-08 14:03:58 +00:00
|
|
|
} else {
|
2016-09-22 10:00:33 +00:00
|
|
|
return null
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
2016-08-08 14:00:14 +00:00
|
|
|
}
|
|
|
|
|
2016-07-31 12:36:28 +00:00
|
|
|
/**
|
|
|
|
* run function for each item in Objectmap
|
|
|
|
*/
|
2016-08-08 15:29:44 +00:00
|
|
|
forEach(functionArg: IObjectmapForEachFunction<T>) {
|
2016-09-21 11:44:11 +00:00
|
|
|
return this.objectArray.forEach(functionArg)
|
|
|
|
}
|
2016-07-31 12:36:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* wipe Objectmap
|
|
|
|
*/
|
2016-08-08 15:29:44 +00:00
|
|
|
wipe() {
|
2016-09-21 11:44:11 +00:00
|
|
|
this.objectArray = []
|
2016-07-30 22:54:46 +00:00
|
|
|
}
|
2016-09-29 20:05:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a cloned array of all the objects currently in the Objectmap
|
|
|
|
*/
|
|
|
|
getArray() {
|
|
|
|
return plugins.lodash.cloneDeep(this.objectArray)
|
|
|
|
}
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|