npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

queue-list

v1.1.8

Published

This is a package to create queue data structure

Downloads

84

Readme

queue-list

👉Note: All methods including enqueue, dequeue, peek, isEmpty, size, and clear have constant time complexity 😎 O(1)

queue-list is a JavaScript library for managing a queue data structure. It provides a simple and efficient way to handle elements in a queue with various methods for manipulating and querying the queue. The library also customizes console.log to format Queue instances.

Installation

To install queue-list, use npm:

npm install queue-list

Importing

import Queue from 'queue-list';

Custom Console Logging

The console.log method has been customized to properly handle and format Queue instances. When logging a Queue instance, its toString representation will be used.

Example
import Queue from 'queue-list';

const queue = new Queue(10, "text", { key: 'value' });

console.log(queue); // Output: Front -> |10| |"text"| |{"key":"value"}| <- Rear

queue.dequeue();
console.log(queue); // Output: Front -> |"text"| |{"key":"value"}| <- Rear

queue.clear();
console.log(queue); // Output: Front -> || <- Rear

Detailed Example Use Cases

enqueue(value)

Purpose: Adds a new element to the end of the queue.

Typical Use Case:

import Queue from 'queue-list';

const queue = new Queue(1, "string", true, [1, 2], { key: 'value' });
queue.enqueue(null).enqueue(undefined);
console.log(queue); // Output: "Front -> |1| |"string"| |true| |[1,2]| |{"key":"value"}| |null| |undefined| <- Rear"

Edge Cases:

  1. Enqueue on an empty queue:
const queue = new Queue();
queue.enqueue(10).enqueue("text").enqueue([1, 2, 3]).enqueue({ key: 'value' });
console.log(queue); // Output: "Front -> |10| |"text"| |[1,2,3]| |{"key":"value"}| <- Rear"
  1. Enqueue on a non-empty queue:
const queue = new Queue('initial');
queue.enqueue(42).enqueue({ foo: 'bar' }).enqueue([1, 2]);
console.log(queue); // Output: "Front -> |'initial'| |42| |{"foo":"bar"}| |[1,2]| <- Rear"

dequeue()

Purpose: Removes and returns the element at the front of the queue. Returns null if the queue is empty.

Typical Use Case:

import Queue from 'queue-list';

const queue = new Queue(1, 'text', { key: 'value' });
console.log(queue.dequeue()); // Output: 1
console.log(queue); // Output: "Front -> |'text'| |{"key":"value"}| <- Rear"

Edge Cases:

  1. Dequeue from an empty queue:
const queue = new Queue();
console.log(queue.dequeue()); // Output: null
  1. Dequeue until the queue is empty:
const queue = new Queue('item1', 'item2');
console.log(queue.dequeue()); // Output: 'item1'
console.log(queue.dequeue()); // Output: 'item2'
console.log(queue.dequeue()); // Output: null

peek()

Purpose: Returns the value of the front element without removing it. Returns null if the queue is empty.

Typical Use Case:

import Queue from 'queue-list';

const queue = new Queue('first', 42, { key: 'value' });
console.log(queue.peek()); // Output: 'first'

Edge Cases:

  1. Peek on an empty queue:
const queue = new Queue();
console.log(queue.peek()); // Output: null
  1. Peek after several enqueues and dequeues:
const queue = new Queue('item1', 'item2', 'item3');
queue.dequeue(); // Removes 'item1'
console.log(queue.peek()); // Output: 'item2'

isEmpty()

Purpose: Checks if the queue is empty.

Typical Use Case:

import Queue from 'queue-list';

const queue = new Queue(1, 'text');
console.log(queue.isEmpty()); // Output: false
queue.dequeue();
queue.dequeue();
console.log(queue.isEmpty()); // Output: true

Edge Cases:

  1. Check if an empty queue is empty:
const queue = new Queue();
console.log(queue.isEmpty()); // Output: true
  1. Check after adding and removing elements:
const queue = new Queue();
queue.enqueue('data');
console.log(queue.isEmpty()); // Output: false
queue.dequeue();
console.log(queue.isEmpty()); // Output: true

size()

Purpose: Returns the number of elements in the queue.

Typical Use Case:

import Queue from 'queue-list';

const queue = new Queue(1, 'text', [1, 2], { key: 'value' });
console.log(queue.size()); // Output: 4

Edge Cases:

  1. Size of an empty queue:
const queue = new Queue();
console.log(queue.size()); // Output: 0
  1. Size after several operations:
const queue = new Queue('start');
queue.enqueue('middle');
queue.dequeue();
console.log(queue.size()); // Output: 1

clear()

Purpose: Removes all elements from the queue and resets it to an empty state.

Typical Use Case:

import Queue from 'queue-list';

const queue = new Queue(1, 'text', [1, 2]);
queue.clear();
console.log(queue); // Output: "Front -> || <- Rear"
console.log(queue.isEmpty()); // Output: true

Edge Cases:

  1. Clear an empty queue:
const queue = new Queue();
queue.clear();
console.log(queue); // Output: "Front -> || <- Rear"
  1. Clear a queue with elements:
const queue = new Queue('item1', 'item2');
queue.clear();
console.log(queue.size()); // Output: 0

toArray()

Purpose: Converts the queue to an array of elements.

Typical Use Case:

import Queue from 'queue-list';

const queue = new Queue(1, 'text', [1, 2], { key: 'value' });
console.log(queue.toArray()); // Output: [1, 'text', [1,2], {"key":"value"}]

Edge Cases:

  1. Convert an empty queue to an array:
const queue = new Queue();
console.log(queue.toArray()); // Output: []
  1. Convert a queue with various elements:
const queue = new Queue('string', 42, { foo: 'bar' }, [1, 2]);
console.log(queue.toArray()); // Output: [ 'string', 42, { foo: 'bar' }, [ 1, 2 ] ]

getType()

Purpose: Returns the type of data structure.

Typical Use Case:

import Queue from 'queue-list';

const queue = new Queue();
console.log(queue.getType()); // Output: "queue"

Edge Cases:

  1. Type of an empty queue:
const queue = new Queue();
console.log(queue.getType()); // Output: "queue"
  1. Type after various operations:
const queue = new Queue();
queue.enqueue('element');
console.log(queue.getType()); // Output: "queue"

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Acknowledgments

This package is inspired by the need for a simple and efficient queue implementation in JavaScript. Feel free to use and contribute to make this package even better! If you encounter any issues or have suggestions for improvement, please open an issue on the GitHub repository.

Here is the GitHub Repo Link.