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

@typinghare/stack-queue

v1.0.0

Published

A lightweight library implementing Stack and Queue for TypeScript.

Downloads

4

Readme

stack-queue

This is a lightweight library implementing Stack and Queue for TypeScript. Install it with the following command.

npm i @typinghare/stack-queue

I know that many JavaScript developers can flexibly use Array to implement all functions of stack and queue. Nevertheless, TypeScript developers are not satisfied with Array because readers can hardly identify it as a stack or a queue through the given type definition. As to my own part, I strongly want my code to be neat, readable, and robust. Therefore, I wrote this package, and I hope this can do a favor to some of you guys.

This package has been tested by Jest, with 100% coverage.

Get Started

This package contains two commonly used classes—Stack and Queue—with some easy-to-operate methods. A simple example of a stack is as follows.

import { Stack } from '@typinghare/stack-queue';

const smallBag = new Stack<string>();

// push items on the top of the stack
smallBag.push('clothes');
smallBag.push('textbooks');
smallBag.push('biscuit');

// Pop the top item from the stack. This item will be removed.
console.log(smallBag.pop());  // >> biscuit

// Peek the top item from the stack. This item will not be removed.
console.log(smallBag.peek()); // >> textbooks

// Search an item from the stack.
console.log(smallBag.search('clothes'));  // >> 2

The Queue is similar to it.

import { Queue } from '@typinghare/stack-queue';

const line = new Queue<string>();

// enqueue items to the queue
line.enqueue('Reina');
line.enqueue('Jack');
line.enqueue('Bob');

// dequeue an item
console.log(line.dequeue());  // >> Reina

// Peek the head of the queue. This item will not be removed.
console.log(line.peek());   // >> 'Jack'

// Search an item from the queue.
console.log(line.search('Jack'));  // >> 1

Go Further

Basic Methods

These two classes are basically copied from the Java source code. I notice that Stack and Queue are written by two different guys, and there are some incoordinations within them, so I adjusted some details and make them perfect.

| Operation | Throw Exception if Empty | Return null if Empty | | --------- | ------------------------ |--------------------| | Insert | push() \ enqueue() | | | Remove | pop() \ dequeue() | poll() | | Examine | element() | peek() |

As shown in the table above, method poll() returns and removes the top element of a stack or the head element of a queue, or returns null if it is empty; element() retrieves but does not remove, the top element of a stack or the head element of a queue, and throws an exception if it is empty; peek does as element() but returns null if it is empty.

Besides, you can use size() to count the number of elements in a stack or a queue; use empty() to test if a stack of a queue is empty.

Quick Initialization

When constructing a stack or a queue, you can feed an iterable argument for a quick initialization as follows. The given elements will be added to the class in iterable order.

const stack = new Stack<number>([1, 2, 3]);
const queue = new Queue<string>(new Set(['a', 'b', 'c']));

Pop or Dequeue Multiple Times at one Invocation

You can pass a number to pop() or dequeue() and execute pop or dequeue for specified times at one invocation. The method will return the last item or throw an exception if it is empty when at the last execution.

const stack = new Stack<number>();
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.pop(3));	// >> 10
console.log(stack.size());	// >> 0

The dequeue() is the like.

Search and Find

You can search for an item from a stack or a queue. Notice that the return value is the 1-based depth of the item, so it counts from 1, not 0. The method will return -1 if the item is not in it.

const stack = new Stack<number>();
stack.push(10);
stack.push(20);
stack.push(30);
console.log(stack.search(10));	// >> 3
console.log(stack.search(40));	// >> -1

You can also use find() to find an item from a stack or a queue with a predicate. The method will return -1 if the item satisfied is not in it.

const queue = new Queue<string>();
queue.enqueue('apple');
queue.enqueue('banana');
queue.enqueue('cat');
queue.enqueue('dog');

console.log(queue.find(word => word.at(0) === 'c'));	// >> 3
console.log(queue.find(word => word.at(0) === 'e'));	// >> -1

Epilogue and Some Grumbles

I am accustomed to leaving some thoughts at the end of the documentation. NPM is a huge community and owns a large number of excellent packages. But there is not enough specification and developers follow their ideas at their like, which I think is a little bit debatable. When I was writing this package I kept thinking that why do I have to write this basic library by consuming so much time? But I could not find one that is eligible for my requirements. Many of them are old and do not support TypeScript. I really wish that one day in the future, TypeScript can become an independent language and use its own interpreter and VM, and provides more basic classes like Java. To be honest, I don't like Java's syntax, which is really bloated.

It's the eighth month since I moved to America. I am studying at a community college and FASFA said I am not eligible for financial aid. My family is experiencing a severe deficit problem and I am still unemployed. People say American companies will not accept my out-of-country diploma and thus I cannot find a good job for the time being. I cannot handle the exaggerated inflation and don't see any hope for my desolate future. I wanna suicide, but programming keeps me alive. See how many libraries and projects I can finish before the end of my fragile life. : )