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.
npm install iterable-stack
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);
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
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
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'.
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.
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
top
- return the value of the top element of the stack
size
- return the number of element of the stack.
toArray
- return a new array with al the element of the stack
push(value)
- the push method receives a value and adds it to the "top" of the stack.
pop
- The pop method removes the element at the "top" of the stack and returns its value.
MIT © Gerardo Manuel Chávez Larios contact@gerrylarios.com