Represents an immutable list of items. The List
class provides a variety of
methods to perform operations on lists such as mapping, filtering, and reducing.
This class is designed to offer a functional approach to list manipulation.
List(array: ReadonlyArray<T>)
Creates an instance of List
from a ReadonlyArray
.
This constructor is private and is only meant to be used internally by the class.
Appends new elements to the end of the list and returns a new List
.
Retrieves the element at the specified index. Negative indices count from the end of the list.
Wraps the result in an Option<T>
, which is Some<T>
if an element is found, or None
if the index is out of bounds.
Splits the list into chunks of the specified size and returns a new list of these chunks.
Creates a shallow or deep clone of the list. structuredClone
must be available in the environment to perform a deep clone.
Filters out null
and undefined
values from the list and returns a new list of all truthy values
as well as 'falsy' values like 0 (zero), '' (empty string), and false
that are not null
or undefined
.
compactMap<U>(callback: () => U): List<NonNullable<U>>
Maps each item using a callback function and filters out any null or undefined values in the same pass.
Concatenates all the elements in the provided lists into a single list.
Groups the elements of the list according to a callback function and returns the count of elements in each group.
difference(other: List<T>): List<T>
Returns a new list containing only the elements from this list that are not present in the provided list.
Drops the first count
elements from the list and returns a new list containing the remaining elements.
Drops the first element from the list and returns a new list containing the remaining elements.
Drops the last element from the list and returns a new list containing the remaining elements.
Drops elements from the list as long as the predicate returns true and returns a new list with the remaining elements.
Performs the specified action for each element in the list.
Creates a new list containing tuples of each element in the list and its index.
Determines whether all elements of the list satisfy the specified condition.
Finds the first element in the list satisfying a predicate, if any.
Wraps the result in an Option<T>
, which is Some<T>
if an element is found that satisfies the predicate, or None
if no such element exists.
Finds the index of the first element in the list that satisfies the provided testing function.
Wraps the result in an Option<number>
, which is Some<number>
if an element satisfying the predicate is found,
or None
if no such element exists.
Finds the last element in the list satisfying a predicate, if any.
Wraps the result in an Option<T>
, which is Some<T>
if an element is found that satisfies the predicate from the end of the list,
or None
if no such element exists.
Finds the index of the last element in the list that satisfies the provided testing function.
Wraps the result in an Option<number>
, which is Some<number>
if an index is found that satisfies the predicate from the end of the list,
or None
if no such index exists.
Returns the first element of the list wrapped in an Option<T>
. If the list is empty, it returns None
.
Flattens the list up to a specified depth and returns a new list.
Maps each element using a callback function and flattens the result into a new list.
Groups the elements of the list according to a callback function and returns an object.
Inserts an item at the specified index and returns a new list. Negative indices count from the end of the list.
intersection(other: List<T>): List<T>
Creates a new list that is the intersection of the current list and another list.
Checks if the list is not empty.
Returns the last element of the list wrapped in an Option<T>
. If the list is empty, it returns None
.
Maps each element in the list using a transformation function and returns a new list of the transformed elements.
Moves an element from one position to another within the list and returns a new list. Negative indices count from the end of the list.
Partitions the elements of the list into two groups based on a type-guard predicate. The first group contains all elements for which the predicate returns true (of type U), and the second group contains all the elements for which it returns false (type Exclude<T, U>).
Partitions the elements of the list into two groups based on a boolean-returning predicate. The first group contains all elements for which the predicate returns true, and the second group contains all the elements for which it returns false.
Adds new elements to the beginning of the list and returns a new list.
Returns a random element from the list wrapped in an Option<T>
. If the list is empty, it returns None
.
This method selects a random element using a uniform distribution, meaning each element has an equal chance of being selected.
Reduces the list to a single value using a reducer function and an initial accumulator value.
reduceRight<U>(initialValue: U,callback: (accumulator: U,current: T,) => U,): U
Reduces the list to a single value using a reducer function and an initial accumulator value, starting from the right.
Removes the element at the specified index and returns a new list. Negative indices count from the end of the list.
Replaces the element at the specified index with a new value and returns a new list. Negative indices count from the end of the list.
Rotates the elements of the List by the specified number of positions. Positive values rotate to the right, negative values rotate to the left.
Randomly shuffles the elements of the list a specified number of times and returns a new list. If no number of permutations is specified, the list will be shuffled once by default.
Creates a slice of the list from start
up to, but not including, end
and returns a new list.
Checks if at least one element in the list passes the test implemented by the provided function.
Sorts the elements of the list according to the order specified by the compare function and returns a new list.
Swaps two elements in the list by their indexes and returns a new list.
Returns a new list with elements taken from the beginning as long as the predicate returns true.
Converts the list into a JSON string representation. This method is used by JSON.stringify
.
Creates a new list that is the union of the current list and another list, excluding duplicate elements.
Returns a new list with all duplicate elements removed, based on the result of the callback function.
Updates the element at the specified index using a callback function and returns a new list.
Creates a new List
from an iterable object or array-like object.