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> {
|
2017-06-17 12:44:55 +00:00
|
|
|
(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> {
|
2017-06-17 12:44:55 +00:00
|
|
|
(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> {
|
2017-06-17 12:44:55 +00:00
|
|
|
private objectArray: T[] = []
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a new instance
|
|
|
|
*/
|
|
|
|
constructor () {
|
|
|
|
// nothing here
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add object to Objectmap
|
|
|
|
*/
|
|
|
|
add (objectArg: T) {
|
|
|
|
this.objectArray.push(objectArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* like .add but adds an whole array of objects
|
|
|
|
*/
|
|
|
|
addArray (objectArrayArg: T[]) {
|
|
|
|
for (let item of objectArrayArg) {
|
|
|
|
this.add(item)
|
2016-08-08 14:00:14 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if object is in Objectmap
|
|
|
|
*/
|
|
|
|
checkForObject (objectArg: T) {
|
|
|
|
return this.objectArray.indexOf(objectArg) !== -1
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* find object
|
|
|
|
*/
|
|
|
|
find (findFunction: IObjectmapFindFunction<T>) {
|
|
|
|
let resultArray = this.objectArray.filter(findFunction)
|
|
|
|
if (resultArray.length > 0) {
|
|
|
|
return resultArray[ 0 ]
|
|
|
|
} else {
|
|
|
|
return null
|
2016-11-19 22:08:54 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* finds a specific element and then removes it
|
|
|
|
*/
|
|
|
|
findOneAndRemove (findFunction): T {
|
|
|
|
let foundElement = this.find(findFunction)
|
|
|
|
if (foundElement) {
|
|
|
|
this.remove(foundElement)
|
2016-07-30 22:54:46 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
return foundElement
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* run function for each item in Objectmap
|
|
|
|
*/
|
|
|
|
forEach (functionArg: IObjectmapForEachFunction<T>) {
|
|
|
|
return this.objectArray.forEach(functionArg)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gets an object in the Observablemap and removes it, so it can't be retrieved again
|
|
|
|
*/
|
|
|
|
getOneAndRemove (): T {
|
|
|
|
return this.objectArray.shift()
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* returns a cloned array of all the objects currently in the Objectmap
|
|
|
|
*/
|
|
|
|
getArray () {
|
|
|
|
return plugins.lodash.cloneDeep(this.objectArray)
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if Objectmap ist empty
|
|
|
|
*/
|
|
|
|
isEmpty (): boolean {
|
|
|
|
if (this.objectArray.length === 0) {
|
|
|
|
return true
|
|
|
|
} 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 = []
|
|
|
|
for (let item of this.objectArray) {
|
|
|
|
if (item !== objectArg) {
|
|
|
|
replacementArray.push(item)
|
|
|
|
}
|
2016-11-19 22:08:54 +00:00
|
|
|
}
|
2017-06-17 12:44:55 +00:00
|
|
|
this.objectArray = replacementArray
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* wipe Objectmap
|
|
|
|
*/
|
|
|
|
wipe () {
|
|
|
|
this.objectArray = []
|
|
|
|
}
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|