A powerful, flexible, and type-safe implementation of a singly linked list in TypeScript, featuring unique node identifiers, deep cloning, iterable support, and modern APIs like map, filter, and fromArray.
- β Fully type-safe with TypeScript
- π§ Built-in higher-order functions like
map,filter,reduce - π Iterable with
for...ofand spread operator support - 𧬠Deep
clone()of list or nodes - πͺͺ Unique ID generation using
crypto.randomUUID() - π§Ή Auto-cleaning with
clear()and safe error boundaries - π Includes helper methods:
peekHead,peekTail,isEmpty, etc. - π§° Friendly developer APIs and clear error messaging
- π§Ό Readable
prettyPrint()and simpletoArray()
npm install @iroh-kit/linked-list
# or
yarn add @iroh-kit/linked-listimport { LinkedList } from "@iroh/linked-list";
const list = new LinkedList<number>();
list.append(1);
list.append(2);
list.append(3);
list.map((x) => x * 2).print(); // Prints: 2 β 4 β 6| Method | Description |
|---|---|
append(value) |
Appends value to the end of the list |
prepend(value) |
Prepends value to the beginning |
insert(index, value) |
Inserts value at index or appends if index invalid |
removeAt(index) |
Removes a node at the given index |
remove(callback) |
Removes a node matching the callback |
removeAll(callback) |
Removes all nodes matching callback |
get(index) |
Gets the value at the given index |
update(index, value) |
Updates the node at index |
fromArray(arr) |
Creates a linked list from an array |
toArray() |
Converts list to array |
map(callback) |
Returns new list with callback applied to all values |
filter(callback) |
Returns new list with values matching callback |
find(callback) |
Finds first node matching callback |
indexOf(callback) |
Returns index of node matching callback |
isEmpty() |
Returns true if list has no elements |
size() |
Returns number of nodes in list |
shift() |
Removes head and returns its value |
pop() |
Removes tail and returns its value |
peekHead() |
Returns head node or null |
peekTail() |
Returns tail node or null |
reverse() |
Reverses the list in place |
clone() |
Returns deep clone of the list |
clear() |
Empties the list completely |
[Symbol.iterator]() |
Makes list iterable using for..of or spread |
print() |
Logs simple list as 1 β 2 β 3 |
prettyPrint() |
Logs detailed list nodes with id and value |
| Method | Description |
|---|---|
clone() |
Returns deep clone of the current node |
equals(node) |
Compares this node with another |
isTail() |
Checks if this node is the tail node |
Include your own test framework (e.g. Vitest, Jest) to verify functionality.
npm install --save-dev vitestimport { LinkedList } from "@iroh/linked-list";
import { describe, it, expect } from "vitest";
describe("LinkedList", () => {
it("appends values", () => {
const list = new LinkedList<number>();
list.append(1);
list.append(2);
expect(list.toArray()).toEqual([1, 2]);
});
});npx vitest runIf you're using VS Code or any IDE with TypeScript support, all methods are fully typed and include documentation for autocomplete.
You can also refer to LinkedList.d.ts and Node.d.ts if exposed manually.
- Add doubly linked list support
- Add
sort()method - Add circular linked list variant
- Add
.toJSON()serialization - Add browser devtools visualization tool
GG IROH
πΌ Fullstack Developer
π§ DSA Enthusiast
π§° Focused on clean, reusable, testable code
MIT Β© GG IROH
Use freely, credit respectfully. β€οΈ
- Fork the repo
- Create a feature branch:
git checkout -b feature/my-feature