Skip to content

Commit 7433448

Browse files
committed
refactor: cleanup queuing code
Signed-off-by: Jérôme Benoit <[email protected]>
1 parent 2c17456 commit 7433448

File tree

5 files changed

+10
-10
lines changed

5 files changed

+10
-10
lines changed

src/circular-buffer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@ export class CircularBuffer {
8282
* @returns Numbers' array.
8383
*/
8484
public toArray(): number[] {
85-
const array: number[] = []
8685
if (this.empty()) {
87-
return array
86+
return []
8887
}
88+
const array: number[] = new Array<number>(this.size)
8989
let currentIdx = this.readIdx
9090
for (let i = 0; i < this.size; i++) {
91-
array.push(this.items[currentIdx])
91+
array[i] = this.items[currentIdx]
9292
currentIdx = currentIdx === this.maxArrayIdx ? 0 : currentIdx + 1
9393
}
9494
return array
@@ -105,9 +105,9 @@ export class CircularBuffer {
105105
`Invalid circular buffer size: '${size.toString()}' is not an integer`,
106106
)
107107
}
108-
if (size < 0) {
108+
if (size <= 0) {
109109
throw new RangeError(
110-
`Invalid circular buffer size: ${size.toString()} < 0`,
110+
`Invalid circular buffer size: ${size.toString()} <= 0`,
111111
)
112112
}
113113
}

src/queues/abstract-fixed-queue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export abstract class AbstractFixedQueue<T> implements IFixedQueue<T> {
4747

4848
/** @inheritdoc */
4949
public get(index: number): T | undefined {
50-
if (this.empty() || index >= this.size) {
50+
if (this.empty() || index < 0 || index >= this.size) {
5151
return undefined
5252
}
5353
index += this.start

src/queues/priority-queue.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ export class PriorityQueue<T> {
4040
`Invalid bucket size: '${bucketSize.toString()}' is not an integer`,
4141
)
4242
}
43-
if (bucketSize < 0) {
44-
throw new RangeError(`Invalid bucket size: ${bucketSize.toString()} < 0`)
43+
if (bucketSize <= 0) {
44+
throw new RangeError(`Invalid bucket size: ${bucketSize.toString()} <= 0`)
4545
}
4646
this.bucketSize = bucketSize
4747
this.priorityEnabled = enablePriority

tests/circular-buffer.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Circular buffer test suite', () => {
2727
new TypeError("Invalid circular buffer size: '0.25' is not an integer"),
2828
)
2929
expect(() => new CircularBuffer(-1)).toThrow(
30-
new RangeError('Invalid circular buffer size: -1 < 0'),
30+
new RangeError('Invalid circular buffer size: -1 <= 0'),
3131
)
3232
expect(() => new CircularBuffer(Number.MAX_SAFE_INTEGER + 1)).toThrow(
3333
new TypeError(

tests/queues/priority-queue.test.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('Priority queue test suite', () => {
1212
new TypeError("Invalid bucket size: '' is not an integer"),
1313
)
1414
expect(() => new PriorityQueue(-1)).toThrow(
15-
new RangeError('Invalid bucket size: -1 < 0'),
15+
new RangeError('Invalid bucket size: -1 <= 0'),
1616
)
1717
let priorityQueue = new PriorityQueue()
1818
expect(priorityQueue.bucketSize).toBe(defaultBucketSize)

0 commit comments

Comments
 (0)