@dandre3000/list
v0.1.3
Published
A double linked list class with an API similar to the Array class and a corresponding list node class.
Downloads
5
Maintainers
Readme
list
Exports a List class that represents a double linked list with an API based on the Array class, and a ListNode class that represents a node that may be contained in a List.
Installation
npm install @dandre3000/listUsage
import { List, ListNode } from '@dandre3000/list'
// Add values
const list = new List(1, 2, 3)
list.push(4, 5)
list.unshift(0)
// Remove values
list.pop()
list.shift()
// Iterate values
for (const node of list) {
console.log(node.value) // 1, 2, 3, 4
}
// Access nodes
const firstNode = list.first
const lastNode = list.last
const nodeAt2 = list.at(2)
// Node operations
console.log(nodeAt2 === lastNode.previous) // true
new ListNode(6).appendTo(lastNode)
// Iterative methods
list.filter(({ value }) => value & 1 ^ 1)
.map(({ value }) => value * 2)
// Conversion methods
list.nodes()
list.values()
console.log(list.toString()) // 2,6API
Click here to view full type definitions
ListNode
Instance Properties
value: T- The value stored in the node
list: List<T> | null- The list containing this node or
nullif not in a list
- The list containing this node or
previous: ListNode<T> | null- The previous node in the list or
null
- The previous node in the list or
next: ListNode<T> | null- The next node in the list or
null
- The next node in the list or
Instance Methods
constructor(value: T)- Creates a
ListNodeinstance with the given value
- Creates a
constructor(value: T, list: List<T>, index: number)- Creates a
ListNodeand inserts it into the given list at the specified index
- Creates a
prependTo(node: ListNode<T>): this- Removes this node from its list and prepends it to another node
appendTo(node: ListNode<T>): this- Removes this node and appends it to another node
insertInto(list: List<T>, index: number): this- Removes this node and inserts it into another list at the specified index
remove(): this- Removes this node from its containing list
List
Static Methods
from<T>(items: Iterable<T>): List<T>- Creates a
Listinstance from an iterable
- Creates a
from<T, U>(items: Iterable<T>, mapFn: (element: T, index: number) => U, self: any): List<U>- Creates a
Listinstance with the mapped values of an iterable
- Creates a
fromAsync<T>(items: Iterable<T> | AsyncIterable<T>): List<T>- Asynchronously creates a
Listinstance from an iterable
- Asynchronously creates a
fromAsync<T, U>(items: Iterable<T> | AsyncIterable<T>, mapFn: (element: T, index: number) => U, self: any): List<U>- Asynchronously creates a
Listinstance with the mapped values of an iterable
- Asynchronously creates a
Instance Properties
first: ListNode<T>- The first node or
nullif empty
- The first node or
last: ListNode<T>- The last node or
nullif empty
- The last node or
length: number- The length of the list
Instance Methods
constructor(length: number)- Creates a List with specified length, values are undefined
constructor(...values: T[])- Creates a
Listand inserts the given values
- Creates a
at(index: number): ListNode<T> | null- Returns the node at the given index or
nullif index is out of bounds
- Returns the node at the given index or
unshift(...values: T[]): number- Adds values to the front and returns new length
push(...values: T[]): number- Adds values to the end and returns new length
insert(index: number, ...values: T[]): number- Inserts values at the specified index and returns new length
shift(): ListNode<T>- Removes the first node and return it or
nullif empty
- Removes the first node and return it or
pop(): ListNode<T>- Removes the last node and return it or
nullif empty
- Removes the last node and return it or
remove(index: number): ListNode<T>- Removes and returns node at index
clear(): this- Removes all nodes
splice(start: number, end: number): List<T>- Moves a range of nodes into a new list
splice(start: number, end: number, list: List<T>, index: number): List<T>- Moves a range of nodes into another list at index
fill(value: T): this- Sets all node values to the given value
reverse(): this- Reverses the list order
copyWithin(start: number, end: number, target: number, targetEnd?: boolean): this- Copies values within list ranges
sort(callback): this- Sorts values in the list
flat(depth?: number): this- Concatenate nested list values into the list
slice(start?: number, end?: number): List<T>- Returns a copy of a portion of the list
includes(value: T, backwards?: boolean): boolean- Returns
trueif value is contained in the list
- Returns
indexOf(value: T, backwards?: boolean): number- Returns the index of the value or
-1
- Returns the index of the value or
find(callback, self?, backwards?): ListNode<T>- Finds the first node where callback returns
true
- Finds the first node where callback returns
findIndex(callback, self?, backwards?): number- Finds the index of the first node where callback returns
true
- Finds the index of the first node where callback returns
some(callback, self?, backwards?): boolean- Returns
trueif any node matches callback
- Returns
every(callback, self?, backwards?): boolean- Returns
trueif all nodes match callback
- Returns
reduce(callback, initialValue, self?, backwards?): U- Reduces nodes using callback
filter(callback, self?, backwards?): this- Removes nodes where callback returns
true
- Removes nodes where callback returns
map(callback, self?, backwards?): List<U>- Maps nodes to new values in place
forEach(callback, self?, backwards?): this- Calls callback for each node
concat(...values: (T | List<T>)[]): List<T>- Returns a new
Listwith the given values appended
- Returns a new
nodes(): ListNode<T>[]- Returns
Arrayof all nodes
- Returns
values(): T[]- Returns
Arrayof all node values
- Returns
toString(): string- String representation of the list
[Symbol.iterator](): Iterator<T>- Iterable protocol for the list
