@santi100/linked-list v0.0.4
Santi's Minimal Linked List
This is a minimal, lightweight, and portable linked list library. It's a simple solution for people that don't like stuff like yallist.
It's written in TypeScript and is compatible with any runtime that supports ES3 or higher.
Its index file is an ESM wrapper, so if you're using version 0.0.1, you'll have to import
from cjs/ in order to use the CommonJS version.
Example:
const { LinkedList } = require('@santi100/linked-list/cjs'); // 0.0.1
const { LinkedList } = require('@santi100/linked-list'); // 0.0.2 and higherInstallation
- NPM:
npm install @santi100/linked-list - Yarn:
yarn add @santi100/linked-list - PNPM:
pnpm install @santi100/linked-list
Usage
interface LinkedListItem<T>: The shape of a linked list item.previous: LinkedListItem<T>;The previous item in the linked list. If this is the genesis item, it'snull.value: T;The value of this item in the linked list.
class LinkedList<T = unknown>: This is the linked list class.constructor LinkedList<T = unknown>(iter?: T[]): LinkedList<T>: Creates a new linked list. | Name | Type | Description | Optional? | Default | |------------|--------------|-------------------------------------------|--------------|--------------| |T| type param | The type of every item of the list. | Yes |unknown| |iter?|T[]| An optional array to initialize the list with. | Yes |[]|push(...items: T[]): this;Pushes one or more items to the list. Returns thethisobject for chaining. | Name | Type | Description | Optional? | Default | |------------|--------------|-------------------------------------------|--------------|--------------| |...items|T[]| The item(s) you want to add to the list. | rest param |[]|pop(): this;Pops the last item out of the list (and obviously returns it).close(): this;Closes the list so it can't be modified. Returns thethisobject for chaining.getLength(): this;Retrieves the length of the list.isClosed(): boolean;Returnstruein case the list has been closed (withLinkedList.prototype.close()).reverse(): this;(since 0.0.2) Reverses the order of the items in the linked list in-place. It throwsErrorif the linked list has been closed, and returns the modified linked list instance.toArray(): T[];(since 0.0.2) Returns an array containing the values of each item in the linked list in the order they appear.toString(): string;(since 0.0.2) Returns a JSON representation of the linked list items.peekLast(): LinkedListItem<T> | null;(since 0.0.2) Returns the last item in the linked list without removing it, ornullif the list's empty.peekFirst(): LinkedListItem<T> | null;(since 0.0.2) Returns the first item in the linked list without removing it, ornullif the list's empty.peekList(): LinkedListItem<T>[];(since 0.0.2) Returns a new array containing the same items as the linked list in the order they appear.clear(): this;(since 0.0.3) Removes all items from the linked list. Returns the currentLinkedListinstance.insert(index: number, item: T): this;(since 0.0.3) Inserts an item at the specified index in the linked list. | Name | Type | Description | Optional? | Default | |------------|--------------|-------------------------------------------|--------------|--------------| |index|number| The index at which to insert the item. | No | N/A | |item|T| The item to insert. | No | N/A |remove(value: T): boolean;(since 0.0.3) Removes the first occurrence of the specified item from the linked list. | Name | Type | Description | Optional? | Default | |------------|--------------|-------------------------------------------|--------------|--------------| |value|T| The value of the item to remove. | No | N/A |Returns
trueif the item was removed,falseotherwise.copy(): LinkedList<T>;(since 0.0.4) Copies this linked list. Returns a copy of this linked list.forEach(cb: LinkedListForEachCb<T, R>): this;(since 0.0.4) Executescbfor every item in the list. | Name | Type | Description | Optional? | Default | |------|---------------------------|----------------------------------|--------------|--------------| |cb|LinkedListForEachCb<T, R>| The callback to be executed for every item in the list. | No | N/A |map(cb: LinkedListForEachCb<T, R>): LinkedList<T>;(since 0.0.4) Maps every item of this linked list to another one in a new linked list, viacb. | Name | Type | Description | Optional? | Default | |------|---------------------------|----------------------------------|--------------|--------------| |cb|LinkedListForEachCb<T, R>| The callback to be executed for every item in the original list. | No | N/A | Returns a new linked list containing the results of callingcbfor every item in the original one.filter(cb: LinkedLinkedForEach<T, boolean>): LinkedList;(since 0.0.4) Executescbfor every item in the linked list, and creates a new one which contains only the items that makecbreturntrue.some(cb: LinkedLinkedForEach<T, boolean>): boolean;(since 0.0.4) Returns whether or not at least one item of the linked list makescbreturntrue. | Name | Type | Description | Optional? | Default | |------------|--------------|-------------------------------------------|--------------|--------------| |cb|LinkedLinkedForEach<T, boolean>| The callback function to be executed on every item of the linked list. | No | N/A |