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

iterable-stack

v1.0.2

Published

Simple Stack data structure with ES6 iterable interface

Downloads

2

Readme

iterable-stack

Simple Stack data structure with ES6 iterable interface

Stack is a linear data structure that store information in the form of a list. They allow only adding and removing elements under a LIFO pattern (last in, first out). Which means that insertions and deletions are performed at the end, called top of the stack.

Install

npm install iterable-stack

Usage

Create a new Stack object

You can create an empty stack.

import Stack from 'stack';

const stack = new Stack();

And you can create an stack with elements.

import Stack from 'stack';

const stack = new Stack(1, 2, 3);

Add (push) new elements to the Stack

You can use the push method to add a new element;

const stack = new Stack('A', 'B', 'C');

stack.push('D');

stack.size // 4

It supports chaining mechanism:

const stack = new Stack();

stack.push('A').push('B').push('C');

stack.size // 3

Remove (pop) elements from the Stack

You can use the pop method to get and remove the last added element of the Stack.

import Stack from 'stack';

const stack = new Stack('A', 'B', 'C') // size = 3;
stack.pop() // 'C' and size = 2

Access to the top value:

Another way to get the top values is with the property top.

import Stack from 'stack';

const stack = new Stack('A', 'B', 'C') // size = 3;
stack.top // 'C'.

From stack to array.

The stack object has a property called toArray that returns an new array based on the elements of the stack.

import Stack from 'stack';

const stack = new Stack('A', 'B', 'C') // size = 3;
stack.toArray // ['C', 'B', 'A'].

If you want to implement more actions or transform the Stack object into another kind of iterable, please read the section below.

Iterable

The Stack object is an iterable, because implements the [Symbol.iterator]() protocol, that means that you can perform any actions with constructs. NOTE: this methods don't remove any element of the stack.

Destructuring:

const stack = new Stack(1).push(2).push(3);
const [first, second, third] = stack;

// first = 3
// second = 2
// third = 1

For of loop:

const stack = new Stack(1, 2, 3);

for (const iterator of stack) {
  // code
}

Array.from:

const stack = new Stack(1).push(2).push(3);

Array.from(stack) // [3, 2, 1]

Spread operator:

const stack = new Stack(1).push(2).push(3);

[...stack] = [3, 2, 1];

Constructor of Set:

const stack = new Stack(1);
const set = new Set(stack);

set.has(1) // true

Promise.all:

const promise = new Promise((resolve) => resolve(1));
const stack = new Stack(promise);

Promise.all(stack); // resolves to [1]

Promise.race:

const promise = new Promise((resolve) => resolve(1));
const stack = new Stack(promise);

Promise.race(stack); // resolves to 1

API

top

top - return the value of the top element of the stack

size

size - return the number of element of the stack.

toArray

toArray - return a new array with al the element of the stack

push

push(value) - the push method receives a value and adds it to the "top" of the stack.

pop

pop - The pop method removes the element at the "top" of the stack and returns its value.

License

MIT © Gerardo Manuel Chávez Larios [email protected]