es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
84 lines (83 loc) • 2.31 kB
JavaScript
/**
* Represents a queue data structure.
* @template T The type of elements in the queue.
* @example
* const queue = new Queue<number>();
* queue.enqueue(1).enqueue(2);
* console.log(queue.dequeue()); // 1
*/
export class Queue {
items = [];
/**
* Adds an item to the end of the queue.
* @param {T} item - The item to add to the queue.
* @returns {Queue<T>} The queue instance for chaining.
*/
enqueue(item) {
this.items.push(item);
return this;
}
/**
* Removes and returns the item at the front of the queue.
* @returns {T} The item at the front of the queue.
* @throws {Error} If the queue is empty.
*/
dequeue() {
if (this.isEmpty()) {
throw new Error('Queue is empty.');
}
return this.items.shift();
}
/**
* Returns the number of items in the queue.
* @returns {number} The size of the queue.
*/
size() {
return this.items.length;
}
/**
* Returns the item at the front of the queue without removing it.
* @returns {T | undefined} The item at the front of the queue, or undefined if the queue is empty.
*/
peek() {
return this.items[0];
}
/**
* Checks if the queue is empty.
* @returns {boolean} True if the queue is empty, false otherwise.
*/
isEmpty() {
return this.items.length === 0;
}
/**
* Checks if the queue contains a specific item.
* @param {T} item - The item to check for.
* @returns {boolean} True if the item is in the queue, false otherwise.
*/
contains(item) {
return this.items.includes(item);
}
/**
* Creates a copy of the queue.
* @returns {Queue<T>} A new queue instance with the same items.
*/
copy() {
const newQueue = new Queue();
newQueue.items = [...this.items];
return newQueue;
}
/**
* Serializes the queue to a JSON string.
* @returns {string} The serialized queue.
*/
serialize() {
return JSON.stringify(this.items);
}
/**
* Deserializes a JSON string to populate the queue.
* @param {string} data - The JSON string to deserialize.
*/
deserialize(data) {
this.items = JSON.parse(data);
}
}