1.0.0 • Published 5 months ago
typesafe-queue v1.0.0
Safe Queue
Safe Queue is a TypeScript library providing a type-safe implementation of a queue data structure. It ensures compile-time safety, preventing common errors associated with incorrect queue usage.
Installation
Install the library via npm:
npm install safe-queue
Usage
Import the library
import { createQueue } from 'safe-queue';
Create a new queue
// Create a queue of numbers
const numberQueue = createQueue<number>();
// Create a queue of strings
const stringQueue = createQueue<string>();
Options
const numberQueue = createQueue<number>((options) => {
options.capacity = 50;
options.initialValues = [1, 2];
});
When creating a queue, there's two options that can be set:
capacity
: The maximum value of elements the queue can hold. ATypeError
will be thrown if this value is not a whole positive integer. The default value isNumber.MAX_VALUE
.initialValues
: Initialize a new queue with a copy of the values specified in this field. The default value is[]
.
Available operations
// Add elements onto the queue
numberQueue.enqueue(42);
numberQueue.enqueue(10);
// Remove elements from the queue
const poppedElement = numberQueue.dequeue();
console.log(poppedElement); // Output: 42
// Check queue size
const size = numberQueue.length;
console.log(size); // Output: 1
// Peek the next element
const peekedElement = numberQueue.peek();
console.log(peekedElement); // Output: 42
// Check if a certain element is present in the queue
const isElementPresentInTheQueue = numberQueue.includes(25);
console.log(isElementPresentInTheQueue); // Output: false
// Clear the queue
numberQueue.clear();
Contributing
We welcome contributions! To contribute to Safe Queue, follow these steps:
- Fork the repository
- Clone your fork:
git clone https://github.com/your-username/safe-queue.git
- Create a new branch:
git checkout -b your-feature
- Make your changes and commit them:
git commit -m 'Add your feature'
- Push to the branch:
git push origin your-feature
- Submit a pull request
Please ensure that your code adheres to the existing coding standards and includes tests.
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
1.0.0
5 months ago