lik/ts/lik.objectmap.ts

86 lines
1.7 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[] = []
2016-08-07 16:07:21 +00:00
/**
* returns a new instance
*/
constructor() {
2016-08-07 16:07:21 +00:00
}
2016-08-07 16:07:21 +00:00
2016-07-31 12:36:28 +00:00
/**
* add object to Objectmap
*/
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
*/
remove(objectArg: T) {
let replacmentArray = []
for (let item of this.objectArray) {
if (item !== objectArg) {
replacmentArray.push(item)
2016-07-30 22:54:46 +00:00
}
}
this.objectArray = replacmentArray
}
2016-07-31 12:36:28 +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 {
2016-09-22 10:00:33 +00:00
return null
}
}
2016-07-31 12:36:28 +00:00
/**
* run function for each item in Objectmap
*/
forEach(functionArg: IObjectmapForEachFunction<T>) {
return this.objectArray.forEach(functionArg)
}
2016-07-31 12:36:28 +00:00
/**
* wipe Objectmap
*/
wipe() {
this.objectArray = []
2016-07-30 22:54:46 +00:00
}
}