lik/ts/lik.limitedarray.ts

43 lines
935 B
TypeScript
Raw Normal View History

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