update tests
This commit is contained in:
@ -1,121 +1,121 @@
|
||||
import * as plugins from './lik.plugins'
|
||||
|
||||
export interface IObjectmapForEachFunction<T> {
|
||||
(itemArg: T): void
|
||||
(itemArg: T): void
|
||||
}
|
||||
|
||||
export interface IObjectmapFindFunction<T> {
|
||||
(itemArg: T): boolean
|
||||
(itemArg: T): boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* allows keeping track of objects
|
||||
*/
|
||||
export class Objectmap<T> {
|
||||
private objectArray: T[] = []
|
||||
private objectArray: T[] = []
|
||||
|
||||
/**
|
||||
* returns a new instance
|
||||
*/
|
||||
constructor() {
|
||||
/**
|
||||
* 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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* add object to Objectmap
|
||||
*/
|
||||
add(objectArg: T) {
|
||||
this.objectArray.push(objectArg)
|
||||
}
|
||||
/**
|
||||
* check if object is in Objectmap
|
||||
*/
|
||||
checkForObject (objectArg: T) {
|
||||
return this.objectArray.indexOf(objectArg) !== -1
|
||||
}
|
||||
|
||||
/**
|
||||
* like .add but adds an whole array of objects
|
||||
*/
|
||||
addArray(objectArrayArg: T[]) {
|
||||
for (let item of objectArrayArg) {
|
||||
this.add(item)
|
||||
}
|
||||
/**
|
||||
* find object
|
||||
*/
|
||||
find (findFunction: IObjectmapFindFunction<T>) {
|
||||
let resultArray = this.objectArray.filter(findFunction)
|
||||
if (resultArray.length > 0) {
|
||||
return resultArray[ 0 ]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* check if object is in Objectmap
|
||||
*/
|
||||
checkForObject(objectArg: T) {
|
||||
return this.objectArray.indexOf(objectArg) !== -1
|
||||
/**
|
||||
* finds a specific element and then removes it
|
||||
*/
|
||||
findOneAndRemove (findFunction): T {
|
||||
let foundElement = this.find(findFunction)
|
||||
if (foundElement) {
|
||||
this.remove(foundElement)
|
||||
}
|
||||
return foundElement
|
||||
}
|
||||
|
||||
/**
|
||||
* find object
|
||||
*/
|
||||
find(findFunction: IObjectmapFindFunction<T>) {
|
||||
let resultArray = this.objectArray.filter(findFunction)
|
||||
if (resultArray.length > 0) {
|
||||
return resultArray[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
/**
|
||||
* run function for each item in Objectmap
|
||||
*/
|
||||
forEach (functionArg: IObjectmapForEachFunction<T>) {
|
||||
return this.objectArray.forEach(functionArg)
|
||||
}
|
||||
|
||||
/**
|
||||
* finds a specific element and then removes it
|
||||
*/
|
||||
findOneAndRemove(findFunction): T {
|
||||
let foundElement = this.find(findFunction)
|
||||
if (foundElement) {
|
||||
this.remove(foundElement)
|
||||
}
|
||||
return foundElement
|
||||
}
|
||||
/**
|
||||
* gets an object in the Observablemap and removes it, so it can't be retrieved again
|
||||
*/
|
||||
getOneAndRemove (): T {
|
||||
return this.objectArray.shift()
|
||||
}
|
||||
|
||||
/**
|
||||
* run function for each item in Objectmap
|
||||
*/
|
||||
forEach(functionArg: IObjectmapForEachFunction<T>) {
|
||||
return this.objectArray.forEach(functionArg)
|
||||
}
|
||||
/**
|
||||
* returns a cloned array of all the objects currently in the Objectmap
|
||||
*/
|
||||
getArray () {
|
||||
return plugins.lodash.cloneDeep(this.objectArray)
|
||||
}
|
||||
|
||||
/**
|
||||
* gets an object in the Observablemap and removes it, so it can't be retrieved again
|
||||
*/
|
||||
getOneAndRemove(): T {
|
||||
return this.objectArray.shift()
|
||||
/**
|
||||
* check if Objectmap ist empty
|
||||
*/
|
||||
isEmpty (): boolean {
|
||||
if (this.objectArray.length === 0) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* returns a cloned array of all the objects currently in the Objectmap
|
||||
*/
|
||||
getArray() {
|
||||
return plugins.lodash.cloneDeep(this.objectArray)
|
||||
/**
|
||||
* remove object from Objectmap
|
||||
*/
|
||||
remove (objectArg: T) {
|
||||
let replacementArray = []
|
||||
for (let item of this.objectArray) {
|
||||
if (item !== objectArg) {
|
||||
replacementArray.push(item)
|
||||
}
|
||||
}
|
||||
this.objectArray = replacementArray
|
||||
}
|
||||
|
||||
/**
|
||||
* check if Objectmap ist empty
|
||||
*/
|
||||
isEmpty(): boolean {
|
||||
if (this.objectArray.length === 0) {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* remove object from Objectmap
|
||||
*/
|
||||
remove(objectArg: T) {
|
||||
let replacementArray = []
|
||||
for (let item of this.objectArray) {
|
||||
if (item !== objectArg) {
|
||||
replacementArray.push(item)
|
||||
}
|
||||
}
|
||||
this.objectArray = replacementArray
|
||||
}
|
||||
|
||||
/**
|
||||
* wipe Objectmap
|
||||
*/
|
||||
wipe() {
|
||||
this.objectArray = []
|
||||
}
|
||||
/**
|
||||
* wipe Objectmap
|
||||
*/
|
||||
wipe () {
|
||||
this.objectArray = []
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user