2018-01-27 17:11:11 +00:00
|
|
|
import * as plugins from "./lik.plugins";
|
2017-09-21 20:54:22 +00:00
|
|
|
|
|
|
|
export class LimitedArray<T> {
|
2018-01-27 17:11:11 +00:00
|
|
|
array: T[] = [];
|
|
|
|
arrayLimit: number;
|
2017-09-21 20:54:22 +00:00
|
|
|
constructor(limitArg: number) {
|
2018-01-27 17:11:11 +00:00
|
|
|
this.arrayLimit = limitArg;
|
2017-09-21 20:54:22 +00:00
|
|
|
}
|
|
|
|
|
2018-01-27 17:11:11 +00:00
|
|
|
addOne(objectArg: T) {
|
|
|
|
this.array.unshift(objectArg);
|
2017-09-21 20:54:22 +00:00
|
|
|
if (this.array.length > this.arrayLimit) {
|
2018-01-27 17:11:11 +00:00
|
|
|
this.array.length = this.arrayLimit;
|
2017-09-21 20:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 17:11:11 +00:00
|
|
|
addMany(objectArrayArg: T[]) {
|
2017-09-21 20:54:22 +00:00
|
|
|
for (let objectArg of objectArrayArg) {
|
2018-01-27 17:11:11 +00:00
|
|
|
this.addOne(objectArg);
|
2017-09-21 20:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 17:11:11 +00:00
|
|
|
setLimit(limitArg: number) {
|
|
|
|
this.arrayLimit = limitArg;
|
2017-09-21 20:54:22 +00:00
|
|
|
if (this.array.length > this.arrayLimit) {
|
2018-01-27 17:11:11 +00:00
|
|
|
this.array.length = this.arrayLimit;
|
2017-09-21 20:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-27 17:11:11 +00:00
|
|
|
getAverage(): number {
|
|
|
|
if (typeof this.array[0] === "number") {
|
|
|
|
let sum = 0;
|
2017-09-21 20:54:22 +00:00
|
|
|
for (let localNumber of this.array) {
|
2018-01-27 17:11:11 +00:00
|
|
|
let localNumberAny: any = localNumber;
|
|
|
|
sum = sum + localNumberAny;
|
2017-09-21 20:54:22 +00:00
|
|
|
}
|
2018-01-27 17:11:11 +00:00
|
|
|
return sum / this.array.length;
|
2017-09-21 20:54:22 +00:00
|
|
|
} else {
|
2018-01-27 17:11:11 +00:00
|
|
|
return null;
|
2017-09-21 20:54:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|