@ch1nm4y-8/simple-stack-js
v1.0.0
Published
A lightweight Stack for JavaScript and TypeScript
Maintainers
Readme
@ch1nm4y-8/simple-stack-js
A lightweight LIFO stack for JavaScript and TypeScript.
Features
- Zero runtime dependencies
- LIFO (Last In, First Out)
- Method chaining support
- Full TypeScript support with type declarations
- Supports ESM (
import) - Supports CommonJS (
require) - Iterable (
for...of,Array.from, spread operator) - Clone support
- Array conversion support
Install
npm install @ch1nm4y-8/simple-stack-jsTypeScript
import { Stack } from "@ch1nm4y-8/simple-stack-js";
const stack = new Stack<number>();
stack.push(1);
stack.push(2);
stack.push(3);
console.log(stack.pop()); // 3
console.log(stack.peek()); // 2
console.log(stack.size()); // 2Constructor
const stack1 = new Stack([1, 2, 3]);
const stack2 = new Stack(new Set([1, 2, 3]));
console.log(stack1.peek()); // 3
console.log(stack2.peek()); // 3Iteration
const stack = new Stack([1, 2, 3]);
for (const item of stack) {
console.log(item);
}Iteration follows insertion order (bottom → top).
Output:
1
2
3Type Safety
const stack = new Stack<number>();
stack.push(1);
// Type Error
stack.push("hello");ECMAScript Modules (ESM)
import { Stack } from "@ch1nm4y-8/simple-stack-js";
const stack = new Stack();
stack.push("A");
stack.push("B");
console.log(stack.pop()); // BCommonJS
const { Stack } = require("@ch1nm4y-8/simple-stack-js");
const stack = new Stack();
stack.push("A");
stack.push("B");
console.log(stack.pop()); // BAPI
| Method | Time Complexity | Description |
| ------------- | --------------- | ---------------------------------------- |
| push(value) | O(1) | Adds an item to the top of the stack |
| pop() | O(1) | Removes and returns the top item |
| peek() | O(1) | Returns the top item without removing it |
| clear() | O(1) | Removes all items |
| size() | O(1) | Returns the number of items |
| isEmpty() | O(1) | Returns whether the stack is empty |
| toArray() | O(n) | Converts the stack to an array |
| clone() | O(n) | Returns a shallow copy of the stack |
push(value)
Adds an item to the top of the stack.
stack.push(1);Method chaining is supported:
stack.push(1).push(2).push(3);Returns:
this;pop()
Removes and returns the top item.
const value = stack.pop();Returns:
T | undefined;peek()
Returns the top item without removing it.
const value = stack.peek();Returns:
T | undefined;clear()
Removes all items from the stack.
stack.clear();Returns:
voidsize()
Returns the number of items in the stack.
console.log(stack.size());Returns:
number;isEmpty()
Returns whether the stack is empty.
console.log(stack.isEmpty());Returns:
boolean;toArray()
Returns the stack contents in insertion order (bottom → top).
const stack = new Stack([1, 2, 3]);
console.log(stack.toArray());Output:
[1, 2, 3];clone()
const s1 = new Stack([1, 2, 3]);
const s2 = s1.clone();
s1.pop();
console.log(s1.toArray()); // [1, 2]
console.log(s2.toArray()); // [1, 2, 3]Example
import { Stack } from "@ch1nm4y-8/simple-stack-js";
const history = new Stack<string>();
history.push("Home");
history.push("Profile");
history.push("Settings");
console.log(history.pop()); // Settings
console.log(history.pop()); // ProfileOutput:
Settings
ProfileLicense
MIT
