limitedArray.ts 525 B

123456789101112131415161718192021222324
  1. /**
  2. * 限制长度的数据,可以理解为限制了最大长度的队列
  3. */
  4. export class LimitedArray {
  5. readonly limit: number = 5;
  6. public items: any[] = [];
  7. constructor(limit: number = 5, initial: any[] = []) {
  8. this.limit = limit;
  9. initial.forEach((item) => {
  10. this.push(item);
  11. });
  12. }
  13. push(...items: any[]) {
  14. items.forEach((item) => {
  15. this.items.push(item);
  16. if (this.items.length > this.limit) {
  17. this.items.shift();
  18. }
  19. });
  20. return this.items.length;
  21. }
  22. }