From a0d415448af2d5a72c5349d3911f1bcdc3ff0fd2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 5 May 2023 00:34:56 +0200 Subject: [PATCH] docs: add comments in queue implementation MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- .eslintrc.js | 1 + src/queue.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/.eslintrc.js b/.eslintrc.js index 58450447..d7004cc5 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -49,6 +49,7 @@ module.exports = defineConfig({ 'ctx', 'deprecations', 'dequeue', + 'dequeued', 'ecma', 'enqueue', 'enum', diff --git a/src/queue.ts b/src/queue.ts index 35f9025e..a2a61381 100644 --- a/src/queue.ts +++ b/src/queue.ts @@ -2,6 +2,8 @@ /** * Queue + * + * @typeParam T - Type of queue items. */ export class Queue { private items: Record @@ -14,16 +16,34 @@ export class Queue { this.tail = 0 } + /** + * Get the size of the queue. + * + * @returns The size of the queue. + * @readonly + */ public get size (): number { return this.tail - this.head } + /** + * Enqueue an item. + * + * @param item - Item to enqueue. + * @returns The new size of the queue. + */ public enqueue (item: T): number { this.items[this.tail] = item this.tail++ return this.size } + /** + * Dequeue an item. + * + * @returns The dequeued item. + * @returns `undefined` if the queue is empty. + */ public dequeue (): T | undefined { if (this.size <= 0) return undefined const item = this.items[this.head] -- 2.34.1