Merge dependabot/npm_and_yarn/examples/typescript/http-server-pool/express-worker_thr...
[poolifier.git] / src / circular-array.ts
CommitLineData
b97b9dc2 1// Copyright Jerome Benoit. 2021-2024. All Rights Reserved.
78099a15 2
dc021bcc 3export const DEFAULT_CIRCULAR_ARRAY_SIZE = 1024
78099a15
JB
4
5/**
4342a45e 6 * Array with a maximum length and shifting items when full.
4bffc062 7 *
9d9fb7b6 8 * @typeParam T - Type of items.
4bffc062 9 * @internal
78099a15
JB
10 */
11export class CircularArray<T> extends Array<T> {
12 public size: number
13
14 constructor (size: number = DEFAULT_CIRCULAR_ARRAY_SIZE, ...items: T[]) {
15 super()
16 this.checkSize(size)
17 this.size = size
18 if (arguments.length > 1) {
19 this.push(...items)
20 }
21 }
22
4888bc9c 23 /** @inheritDoc */
78099a15
JB
24 public push (...items: T[]): number {
25 const length = super.push(...items)
26 if (length > this.size) {
27 super.splice(0, length - this.size)
28 }
29 return this.length
30 }
31
4888bc9c 32 /** @inheritDoc */
78099a15
JB
33 public unshift (...items: T[]): number {
34 const length = super.unshift(...items)
35 if (length > this.size) {
36 super.splice(this.size, items.length)
37 }
38 return this.length
39 }
40
4888bc9c 41 /** @inheritDoc */
78099a15
JB
42 public concat (...items: Array<T | ConcatArray<T>>): CircularArray<T> {
43 const concatenatedCircularArray = super.concat(
44 items as T[]
45 ) as CircularArray<T>
46 concatenatedCircularArray.size = this.size
47 if (concatenatedCircularArray.length > concatenatedCircularArray.size) {
48 concatenatedCircularArray.splice(
49 0,
50 concatenatedCircularArray.length - concatenatedCircularArray.size
51 )
52 }
53 return concatenatedCircularArray
54 }
55
4888bc9c 56 /** @inheritDoc */
4077981d
JB
57 public splice (
58 start: number,
59 deleteCount?: number,
60 ...items: T[]
61 ): CircularArray<T> {
62 let itemsRemoved: T[] = []
1b6e07ba 63 if (arguments.length >= 3 && deleteCount != null) {
4077981d
JB
64 itemsRemoved = super.splice(start, deleteCount, ...items)
65 if (this.length > this.size) {
66 const itemsOverflowing = super.splice(0, this.length - this.size)
67 itemsRemoved = new CircularArray<T>(
68 itemsRemoved.length + itemsOverflowing.length,
69 ...itemsRemoved,
70 ...itemsOverflowing
71 )
72 }
78099a15
JB
73 } else if (arguments.length === 2) {
74 itemsRemoved = super.splice(start, deleteCount)
75 } else {
76 itemsRemoved = super.splice(start)
77 }
4077981d 78 return itemsRemoved as CircularArray<T>
78099a15
JB
79 }
80
81 public resize (size: number): void {
82 this.checkSize(size)
83 if (size === 0) {
84 this.length = 0
85 } else if (size < this.size) {
86 for (let i = size; i < this.size; i++) {
87 super.pop()
88 }
89 }
90 this.size = size
91 }
92
93 public empty (): boolean {
94 return this.length === 0
95 }
96
97 public full (): boolean {
98 return this.length === this.size
99 }
100
101 private checkSize (size: number): void {
102 if (!Number.isSafeInteger(size)) {
103 throw new TypeError(
104 `Invalid circular array size: ${size} is not a safe integer`
105 )
106 }
107 if (size < 0) {
108 throw new RangeError(`Invalid circular array size: ${size} < 0`)
109 }
110 }
111}