feat(collections): add new collection APIs, iterator support, and tree serialization utilities
This commit is contained in:
@@ -7,6 +7,10 @@ export class LimitedArray<T> {
|
||||
this.arrayLimit = limitArg;
|
||||
}
|
||||
|
||||
public get length(): number {
|
||||
return this.array.length;
|
||||
}
|
||||
|
||||
addOne(objectArg: T) {
|
||||
this.array.unshift(objectArg);
|
||||
if (this.array.length > this.arrayLimit) {
|
||||
@@ -28,6 +32,9 @@ export class LimitedArray<T> {
|
||||
}
|
||||
|
||||
getAverage(): number {
|
||||
if (this.array.length === 0) {
|
||||
return 0;
|
||||
}
|
||||
if (typeof this.array[0] === 'number') {
|
||||
let sum = 0;
|
||||
for (let localNumber of this.array) {
|
||||
@@ -39,4 +46,25 @@ export class LimitedArray<T> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
remove(item: T): boolean {
|
||||
const idx = this.array.indexOf(item);
|
||||
if (idx !== -1) {
|
||||
this.array.splice(idx, 1);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
clear(): void {
|
||||
this.array.length = 0;
|
||||
}
|
||||
|
||||
getArray(): T[] {
|
||||
return [...this.array];
|
||||
}
|
||||
|
||||
public [Symbol.iterator](): Iterator<T> {
|
||||
return this.array[Symbol.iterator]();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user