An implementation of a doubly linked list that provides direct access to the cells that store the elements.
Remarks
The cells are represented by class ListCell<T>. Cells can be used to iterate the elements of the list using firstCell and lastCell together with next/ previous, respectively.
This class supports fast access and removal operations. Specifically, it is possible to remove a list cell in constant time.
Note that this class also provides all relevant methods to use the list like a stack data type.
This implementation permits null
as values.
Type Parameters
- T
Type Details
- yFiles module
- core
Constructors
Creates a list that is initialized with the elements provided by the given enumerable.
Parameters
A map of options to pass to the method.
- c - IEnumerable<T>
- The enumerable collection of elements to initialize the list with.
Properties
Gets the first cell of this list.
Preconditions
!isEmpty()
.null
if the list is empty.
Gets the first element of this list.
Whether this collection is read-only.
Implements
Gets the last cell of this list.
Preconditions
!isEmpty()
.null
if the list is empty.
Gets the last element of this list.
Gets a live view of this list as an IEnumerable<ListCell> of ListCell<T>s.
Gets the number of elements in this list.
Overrides
Methods
Adds the given item
to the collection.
Implements
Appends all elements provided by the given collection to this list.
Parameters
A map of options to pass to the method.
- collection - IEnumerable<T>
- The collection of elements to be appended to the list.
Returns
- ↪boolean
- Whether there have been elements appended.
Adds all elements provided by the given collection to this list at the specified position.
Remarks
Parameters
A map of options to pass to the method.
- index - number
- Index at which the first element of the specified collection is placed
- c - IEnumerable<T>
- The collection that contains the elements to be added
Returns
- ↪boolean
true
if this list was changed,false
otherwise
Throws
- Exception({ name: 'IndexOutOfRangeError' })
- If the index is out of range, that is, negative or larger than the size of this list
Inserts the given element at the head of this list.
Parameters
A map of options to pass to the method.
- element - T
- The element to be inserted at the head of the list.
Returns
- ↪ListCell<T>
- The newly created ListCell<T> that stores the given element.
Adds a formerly removed ListCell<T> at the head of this list.
Remarks
Parameters
A map of options to pass to the method.
- cell - ListCell<T>
- A list cell which is not part of any list.
Inserts the given element at the tail of this list.
Parameters
A map of options to pass to the method.
- element - T
- The element to be inserted at the tail of the list.
Returns
- ↪ListCell<T>
- The newly created ListCell<T> that stores the given element.
Adds a formerly removed ListCell<T> at the tail of this list.
Remarks
Parameters
A map of options to pass to the method.
- cell - ListCell<T>
- A list cell which is not part of any list.
Creates a wrapped enumerable that has one or several elements appended to it.
Remarks
items
any special if they are array-like or enumerable-like.Parameters
A map of options to pass to the method.
- items - T
- The item(s) to prepend to the enumeration
Returns
- ↪IEnumerable<T>
- A new live view over the original enumerable that has each of the
items
appended to it.
Examples
const list = List.fromArray([1, 2, 3, 4])
const appendedEnumerable = list.append(5, 6)
console.log(appendedEnumerable.toArray()) // yields [1,2,3,4,5,6]
// show live updates
list.add(4.5)
console.log(appendedEnumerable.toArray()) // yields [1,2,3,4,4.5,5,6]
See Also
Overrides
Returns the element at the given index
in this enumerable.
Remarks
Array.prototype.at
function, with the important difference, that null
will be returned instead of undefined
in the case of an invalid index.Parameters
A map of options to pass to the method.
- index - number
- The index of the element to return. Supports relative indexing from the end of the enumerable when passed a negative index. In other words, if a negative number is used, the returned element will be found by counting back from the end of the enumerable.
Returns
- ↪T?
- The element at the given
index
, ornull
if the index is not within the valid range of this enumerable.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4])
console.log(enumerable.at(0)) // yields 1
console.log(enumerable.at(-1)) // yields 4
console.log(enumerable.at(5)) // yields null!
// using ?? for default values
console.log(enumerable.at(10) ?? 0) // yields 0
// use ! when you know that this is valid
console.log(enumerable.at(0)!.toFixed())
// use optional chaining when you are unsure whether the index is valid
console.log(enumerable.at(20)?.toFixed())
// combine optional chaining and null-coalescing operator when you are unsure
console.log(enumerable.at(30)?.toLocaleString() ?? 'no value')
Overrides
Removes all elements from this list.
Implements
Concatenates this enumerable with the given other elements
in a new dynamic IEnumerable<T>.
Remarks
Parameters
A map of options to pass to the method.
- elements - any
- Rest arguments to be appended. In case the elements are enumerable-like, too, they will be will be enumerated after the others have been enumerated. But this can also be simple objects of type
T
in which case they will be appended to the enumerable just like with append
Returns
- ↪IEnumerable<T>
- A dynamic composite enumerable.
Examples
const enumerable1 = IEnumerable.from([1, 2])
const enumerable2 = IEnumerable.from([3, 4])
console.log(enumerable1.concat(enumerable2).toArray()) // yields [1,2,3,4]
// single values can be used, too
console.log(enumerable1.concat(3, 4).toArray()) // yields [1,2,3,4]
// arrays are flattened
console.log(enumerable1.concat([5, 6], [7, 8]).toArray()) // now contains [1,2,5,6,7,8]
// works with arrays and array-like collections, too
console.log(enumerable1.concat([3, 4]).toArray()) // yields [1,2,3,4]
const list1 = List.fromArray([1, 2])
const list2 = List.fromArray([3, 4])
const result = list1.concat(list2)
console.log(result.toArray()) // yields [1,2,3,4]
list1.add(2.5)
// result changed also
console.log(result.toArray()) // yields [1,2,2.5,3,4]
list2.add(5)
// result changed also
console.log(result.toArray()) // yields [1,2,2.5,3,4,5]
See Also
Overrides
Copies all elements of this collection into the given array.
Parameters
A map of options to pass to the method.
- array - T[]
- The array to copy the elements to.
- arrayIndex - number
- The index in the given array where the first element should be copied to.
Implements
Creates a wrapped enumerable that yields each element only once.
Remarks
equals
and hashCode
methods are used if present.Parameters
A map of options to pass to the method.
- keySelector - function(T):any
- The optional function that determines the key to determine distinctness of the elements. If omitted, the element itself is used.
Signature Details
function(element: T) : any
A function which creates a key for the givenelement
.Parameters
- element - T
- The element to get the key for.
Returns
- any
- The key for the element.
Returns
- ↪IEnumerable<T>
- A new live view over the original enumerable that yields each element with a given key at most once.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 3, 4])
const result = enumerable.distinct()
console.log(result.toArray()) // yields [1,2,3,4]
See Also
Overrides
Creates a wrapped view of an enumerable which drops the given amount of elements.
Parameters
A map of options to pass to the method.
- amount - number
- The number of elements to drop from the beginning. If negative, the elements will be skipped from the end of the enumeration.
Returns
- ↪IEnumerable<T>
- A new live view over the original enumerable that drops
amount
elements.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5, 6, 7])
const result = enumerable.drop(3)
console.log(result.toArray()) // yields [4,5,6,7]
See Also
Overrides
Creates a wrapped view of an enumerable which skips a number of elements at the beginning while the given predicate yields true
.
Remarks
predicate
yields true
for the elements of the original enumerable, the elements are skipped. The new enumeration will start with the first element for which the predicate yields false
. All subsequent elements will be yielded.Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- The predicate function that determines what elements should be skipped. While the predicate yields
true
for the provided item, the element will not be yielded.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
Returns
- ↪IEnumerable<T>
- A new live view over the original enumerable that discards elements at the beginnging of the original enumeration while the predicate yields
true
.
Examples
const enumerable = IEnumerable.from([
'a',
'b',
'c',
'd',
'a',
'b',
'c',
'd',
])
const result = enumerable.dropWhile(
(element) => element.charCodeAt(0) < 'c'.charCodeAt(0),
)
console.log(result.toArray()) // yields ["c", "d", "a", "b", "c", "d"]
See Also
Overrides
Determines whether every element of the enumerable matches the given predicate
.
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- A predicate function which returns
true
if the element matches a condition.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪boolean
true
if all elements of the enumerable match the givenpredicate
,false
otherwise.
Throws
- Exception({ name: 'ArgumentError' })
predicate
isnull
.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4])
console.log(enumerable.every((item) => item < 5)) // true
console.log(enumerable.every((item) => item < 4)) // false
Overrides
Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given predicate
.
Remarks
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- A predicate function which returns
true
if the given element should be included in the resulting enumerable.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪IEnumerable<T>
- A dynamic enumerable of the elements which match the given
predicate
.
Examples
const enumerable = IEnumerable.from([10, 2, 30, 4, 5, 6])
const result = enumerable.filter((item) => item < 5)
console.log(result.toArray()) // yields [2,4]
const list = List.fromArray([10, 2, 30, 4, 5, 6])
const result = list.filter((item) => item < 5)
list.add(1)
// result changed also
console.log(result.toArray()) // yields [2,4,1]
Overrides
filter
<TResult>(predicate: function(T, number, IEnumerable<T>):boolean, thisArg?: any) : IEnumerable<TResult>Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given type guard.
Remarks
Type Parameters
- TResult
- The type of the elements of the returned enumerable. Is equivalent to the type of the type predicate
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
A type predicate function which returns
true
if the given element is of the desired type and should be included in the resulting enumerable. The signature of the predicate must include a type guard, for examplefunction isFish(pet: Fish | Bird): pet is Fish { return (pet as Fish).swim !== undefined }
Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪IEnumerable<TResult>
- A dynamic enumerable of the elements which match the given type guard.
Examples
const list = List.fromArray([1, 'a', 'b'])
list
.filter(
(item: string | number): item is string => typeof item === 'string',
)
.forEach((item: string) => console.log(item.toUpperCase())) // logs "A B"
Defined in
filter
<TResult>(predicate: function(T, number, IEnumerable<T>):boolean, thisArg?: any) : IEnumerable<T & TResult>Returns a dynamic IEnumerable<T> of the elements of this enumerable which match the given type guard.
Remarks
Type Parameters
- TResult
- The type of the elements of the returned enumerable. Is equivalent to the type of the type predicate
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
A type predicate function which returns
true
if the given element is of the desired type and should be included in the resulting enumerable. The signature of the predicate must include a type guard, for examplefunction isFish(pet: Fish | Bird): pet is Fish { return (pet as Fish).swim !== undefined }
Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪IEnumerable<T & TResult>
- A dynamic enumerable of the elements which match the given type guard.
Examples
const list = List.fromArray([1, 'a', 'b'])
list
.filter(
(item: string | number): item is string => typeof item === 'string',
)
.forEach((item: string) => console.log(item.toUpperCase())) // logs "A B"
Defined in
Returns the first element of the enumerable that matches a given predicate
or null
if there is no such element.
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- A predicate function which returns
true
if the given element should be returned.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪T?
- The first element of this enumerable that matches the predicate or
null
if there is no such element.
Examples
const enumerable = IEnumerable.from([10, 2, 3, 9, 5, 6])
console.log(enumerable.find((item) => item < 5)) // yields 2
// no matches with the given predicate
console.log(enumerable.find((item) => item < 2)) // yields null
Overrides
Returns the first ListCell<T> that stores the given element.
Parameters
A map of options to pass to the method.
- element - T
- The element to search for in the list.
Returns
Returns the first ListCell<T> that matches a given predicate.
Parameters
A map of options to pass to the method.
- predicate - function(T):boolean
- The predicate used to evaluate each element in the list.
Signature Details
function(obj: T) : boolean
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.Parameters
- obj - T
- The object to compare against the criteria defined within the method represented by this delegate.
Returns
- boolean
true
if obj meets the criteria defined within the method represented by this delegate; otherwise,false
.
Returns
Searches the enumerable for the first item for which predicate
returns true
and returns its index.
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- A predicate function which returns
true
if an item matches the search conditions.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪number
- The index of the first item for which
predicate
returnstrue
or -1 if there is no such item in the enumerable.
Overrides
Returns the last element of the enumerable that matches a given predicate
or null
if there is no such element.
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- A predicate function which returns
true
if the given element should be returned.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪T?
- The last element of this enumerable that matches the predicate or
null
if there is no such element.
Examples
const enumerable = IEnumerable.from([10, 2, 3, 9, 5, 6])
console.log(enumerable.findLast((item) => item < 5)) // yields 3
// no matches with the given predicate
console.log(enumerable.findLast((item) => item < 2)) // yields null
Overrides
Searches the enumerable for the last item for which predicate
returns true
and returns its index.
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- A predicate function which returns
true
if an item matches the search conditions.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪number
- The index of the last item for which
predicate
returnstrue
or -1 if there is no such item in the enumerable.
Overrides
Returns the first element of this enumerable.
Returns
- ↪T?
- The first matching element of this enumerable or
null
if nothing was found.
Examples
const enumerable = IEnumerable.from([10, 2, 3, 9, 5, 6])
console.log(enumerable.first()) // yields 10
// returns null for empty enumerable
const empty = IEnumerable.from([])
console.log(empty.first()) // yields null
See Also
Overrides
flatMap
<TResult>(selector: function(T, number, IEnumerable<T>):IEnumerable<TResult>, thisArg?: any) : IEnumerable<TResult>Returns a flattened dynamic IEnumerable<T> of this enumerable using the given selector
function which returns an enumerable for each element of this enumerable.
Remarks
Type Parameters
- TResult
- The type of the elements of the result enumerable.
Parameters
A map of options to pass to the method.
- selector - function(T, number, IEnumerable<T>):IEnumerable<TResult>
- A function which returns an enumerable for each element of this enumerable.
Signature Details
function(element: T, index: number, source: IEnumerable<T>) : IEnumerable<TResult>
The selector to use for the IEnumerable<T>.A function which returns a result of type IEnumerable<T> for eachelement
.Parameters
- element - T
- The current element create a result for.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- IEnumerable<TResult>
- A result of type
of TResult
created from theelement
.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪IEnumerable<TResult>
- A flattened dynamic enumerable of the enumerables provided for each element of this enumerable.
Examples
const enumerable = IEnumerable.from<number>([1, 2, 3])
const result = enumerable.flatMap((item) =>
IEnumerable.from<number>([item, item * 2]),
)
console.log(result.toArray()) // yields [1,2,2,4,3,6]
const list = List.fromArray<number[]>([[1], [2], [3]])
const result = list.flatMap((item) =>
IEnumerable.from<number>([item[0] * 2]),
)
list.add([4])
// result changed also
console.log(result.toArray()) // yields [2,4,6,8]
Overrides
Iterates this enumerable and invokes the given function for each element with the element, its index, and this enumerable as arguments.
Parameters
A map of options to pass to the method.
- action - function(T, number, IEnumerable<T>):void
- The function to call for each element.
Signature Details
function(element: T, index: number, source: IEnumerable<T>)
An action to execute for each element of an IEnumerable<T>.Parameters
- element - T
- The current element.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4])
enumerable.forEach((item) => console.log(item)) // 1 2 3 4
// accessing the index
enumerable.forEach((item, index) =>
console.log(`Item #${index} is ${item}`),
)
Overrides
Gets the element at the specified index.
Parameters
A map of options to pass to the method.
- index - number
- The zero-based index of the element to get or set.
Returns
- ↪T
- The element at the specified index.
Throws
- Exception({ name: 'ArgumentError' })
index
is not a valid index in this enumerable.
See Also
Implements
Gets the cell at the given index.
Parameters
A map of options to pass to the method.
- index - number
- The zero-based index of the cell in this list.
Returns
- ↪ListCell<T>
- The cell.
Throws
Returns a "cursor" for enumerating this list.
Remarks
A cursor instance acts like a movable pointer instance across the conceptually linked list of items in this collection. It should be used in those cases where the IEnumerable<T> interface - which is also implemented by this interface - does not suffice.
For regular enumeration and iteration, it does not provide any benefits over the IEnumerable<T> interface, but it can be used to reverse iterate or move back and forth between elements efficiently. Thus, it is generally superior over the index-based access when used for more than a constant number of elements.
Returns
- ↪ICursor<T>
- A "cursor" for enumerating this list.
Implements
Returns an enumerator that iterates through this collection.
Returns
- ↪IEnumerator<T>
- An IEnumerator<T> that can be used to iterate through this collection.
Implements
groupBy
<TKey,TResult>(keySelector: function(T, number, IEnumerable<T>):TKey, resultCreator?: function(TKey, IEnumerable<T>, number):TResult) : IEnumerable<TResult>Groups the elements in the enumerable according to the equality of a key.
Remarks
keySelector
will be used to retrieve a key. All elements with the same key will be provided to the optional resultCreator
function that receives both the key, and an IEnumerable<T> of the elements that match that key to create a resulting object that will be yielded by this enumerable. Note that the resulting enumerable does not necessarily preserve the order of the (first) appearance of their elements in the original sequence. The elements of each group passed to the resultCreator
are in the order of their original appearance.Type Parameters
- TKey
- The type of the key elements.
- TResult
- The type of the created elements.
Parameters
A map of options to pass to the method.
- keySelector - function(T, number, IEnumerable<T>):TKey
- A function which selects from each element the key.
Signature Details
function(element: T, index: number, source: IEnumerable<T>) : TKey
A function which creates a key for the givenelement
.Parameters
- element - T
- The element to get the key for.
- index - number
- The index of the element in the underlying
. - source - IEnumerable<T>
- The underlying
Returns
- TKey
- resultCreator - function(TKey, IEnumerable<T>, number):TResult
- An optional function which transforms each group of elements and their common key into a resulting object. If omitted, the default implementation will create arrays of length 2 where the first element is the key and the second element is an IEnumerable<TSource> of the elements with the corresponding key.
Signature Details
function(key: TKey, group: IEnumerable<T>, index: number) : TResult
A function which transforms a group of elements and their common key into a resulting object.Parameters
- key - TKey
- The key of the provided group.
- group - IEnumerable<T>
- The group of elements with the given key.
- index - number
- The index of the group.
Returns
- TResult
- A resulting object for the given group with the given key.
Returns
- ↪IEnumerable<TResult>
- An enumeration of groups of the original enumerable.
Examples
const enumerable = IEnumerable.from([
{ type: 'A', amount: 1 },
{ type: 'A', amount: 2 },
{ type: 'B', amount: 4 },
])
// without result creator
const groups1 = enumerable.groupBy<string, string>(
(element) => element.type,
)
for (const g of groups1) {
console.log(`Type is ${g[0]} and has ${g[1].length} members.`)
}
// Type is A and has 2 members.
// Type is B and has 1 members.
// with result creator
const groups2 = enumerable.groupBy(
(element) => element.type,
(type, elements) => ({
type,
amount: elements.sum((item) => item.amount),
}),
)
console.log(groups2.toArray()) // yields [{"type":"A","amount":4},{"type":"B","amount":4}]
const val = IEnumerable.ofRange(1, 10)
.groupBy(
(value, index) => index % 2,
(key, items, index) => [`key ${index % 2}`, items.toArray()],
)
.toArray()
// [
// ["key 0",[1,3,5,7,9]],
// ["key 1",[2,4,6,8,10]]
// ]
const enumerable: IEnumerable<{ value: number; key: string }> =
IEnumerable.from([
{ value: 0, key: 'a' },
{ value: 1, key: 'b' },
{ value: 2, key: 'b' },
{ value: 3, key: 'a' },
])
const obj = Object.fromEntries(
enumerable.groupBy(
(item) => item.key,
(key, items) => [key, items.toArray()],
) as Iterable<[unknown, unknown]>,
)
// {
// a: [{value:0, key:a}, {value:3, key:a}],
// b: [{value:1, key:b}, {value:2, key:b}]
// }
Overrides
Whether this list contains the given element.
Parameters
A map of options to pass to the method.
- element - T
- The element to locate in this list.
Returns
- ↪boolean
true
if the list contains the given element,false
otherwise.
Overrides
The index of the given item in the enumerable.
Parameters
A map of options to pass to the method.
- item - T
- The item to search for.
- fromIndex - number
- An optional start index for the search.
Returns
- ↪number
- The index of the given item in the enumerable.
-1
if the item is not in the enumerable.
Overrides
Returns the zero-based index of the given cell in this list.
Parameters
A map of options to pass to the method.
- cell - ListCell<T>
- The ListCell<T> whose index is to be found.
Returns
- ↪number
- The zero-based index of the given cell in this list, or -1 if the cell is not in the list.
Inserts the given element
into this list at the given index
.
Remarks
Parameters
A map of options to pass to the method.
- index - number
- The index that specifies the insertion position.
- element - T
- The element to insert.
Implements
Inserts the given element into this list with respect to a given reference list cell.
Remarks
The (newly created) list cell that stores the element is inserted right after the reference list cell refCell
.
If refCell == null
, the given element is inserted at the head of the list.
Preconditions
refCell
must be part of this list.
Parameters
A map of options to pass to the method.
- element - T
- The element to be inserted.
- refCell - ListCell<T>
- The list cell used to reference the position.
Returns
- ↪ListCell<T>
- The newly created ListCell{T} that stores the
element
.
Inserts the given element into this list with respect to a given reference list cell.
Remarks
The (newly created) list cell that stores the element is inserted right before the reference list cell refCell
.
If refCell == null
, the given element is appended to the list.
Preconditions
refCell
must be part of this list.
Parameters
A map of options to pass to the method.
- element - T
- The element to be inserted.
- refCell - ListCell<T>
- The list cell used to reference the position.
Returns
- ↪ListCell<T>
- The newly created ListCell<T> that stores the
element
.
Inserts a formerly removed ListCell<T> into this list with respect to a given reference list cell.
Remarks
The ListCell<T> is inserted right after the reference list cell refCell
.
Note: If the given ListCell<T> is part of any other list, that list will be corrupted after the insertion.
Preconditions
refCell
must be part of this list.
Parameters
A map of options to pass to the method.
- cellToInsert - ListCell<T>
- A list cell which is not part of any list.
- refCell - ListCell<T>
- The list cell used to reference the position.
Inserts a formerly removed ListCell<T> into this list with respect to a given reference list cell.
Remarks
The ListCell<T> is inserted right before the reference list cell refCell
.
Note: If the given ListCell<T> is part of any other list, that list will be corrupted after the insertion.
Preconditions
refCell
must be part of this list.
Parameters
A map of options to pass to the method.
- cellToInsert - ListCell<T>
- A list cell which is not part of any list.
- refCell - ListCell<T>
- The list cell used to reference the position.
Returns the last element of this enumerable.
Returns
- ↪T?
- The last element of this enumerable, or
null
if the enumerable is empty.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4])
console.log(enumerable.last()) // yields 4
// if the enumerable is empty null will be returned
const empty = IEnumerable.from([])
console.log(empty.last()) // yields null
See Also
Overrides
map
<TResult>(selector: function(T, number, IEnumerable<T>):TResult, thisArg?: any) : IEnumerable<TResult>Returns a dynamic IEnumerable<T> of this enumerable using the given selector
function which returns a new object for each element of this enumerable.
Remarks
Type Parameters
- TResult
- The type of the mapped elements.
Parameters
A map of options to pass to the method.
- selector - function(T, number, IEnumerable<T>):TResult
- A function which converts each element into a new element of the type
TResult
.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : TResult
The selector to use for the IEnumerable<T>.A function which returns a result of typeTResult
for eachelement
.Parameters
- element - T
- The current element create a result for.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- TResult
- A result of type
TResult
created from theelement
.
- thisArg - any
- The optional value to use as
this
when executing theselector
.
Returns
- ↪IEnumerable<TResult>
- A dynamic enumerable of the results of the provided function.
Examples
const enumerable = IEnumerable.from([1, 2, 3])
const result = enumerable.map((item) => item * 2)
console.log(result.toArray()) // yields [2, 4, 6]
// with index
const result2 = enumerable.map((item, index) => item * index)
console.log(result2.toArray()) // yields [0, 2, 6]
// with index and enumerable
const result3 = enumerable.map(
(item, index, enumerable) =>
`Item no ${index} of ${enumerable.size} is ${item}`,
)
console.log(result3.toArray())
// ["Item no 0 of 3 is 1","Item no 1 of 3 is 2","Item no 2 of 3 is 3"]
const list = List.fromArray([1, 2, 3])
const result = list.map((item) => item * 2)
list.add(4)
// result changed also
console.log(result.toArray()) // yields [2,4,6,8]
Overrides
Converts this untyped enumerable into an enumerable with the given type.
Remarks
Parameters
A map of options to pass to the method.
- resultType - typeof Number
- The type of the elements of the result enumerable.
Returns
- ↪IEnumerable<number>
- A typed enumerable.
Overrides
Converts this untyped enumerable into an enumerable with the given type.
Remarks
Parameters
A map of options to pass to the method.
- resultType - typeof String
- The type of the elements of the result enumerable.
Returns
- ↪IEnumerable<string>
- A typed enumerable.
Overrides
Converts this untyped enumerable into an enumerable with the given type.
Remarks
Parameters
A map of options to pass to the method.
- resultType - typeof Boolean
- The type of the elements of the result enumerable.
Returns
- ↪IEnumerable<boolean>
- A typed enumerable.
Overrides
Converts this untyped enumerable into an enumerable with the given type.
Remarks
Type Parameters
- TResult
- The type of the elements of the result enumerable.
Parameters
A map of options to pass to the method.
- resultType - Constructor<TResult>
- The type of the elements of the result enumerable.
Returns
- ↪IEnumerable<TResult>
- A typed enumerable.
Overrides
Removes and returns the last value in this list.
Remarks
Array.pop()
function and returns undefined
if the list is empty.Returns
- ↪T
- The removed value.
undefined
if the list was empty.
Defined in
Creates a wrapped enumerable that has on or several elements prepended to it.
Parameters
A map of options to pass to the method.
- items - T
- The item(s) to prepend to the enumeration
Returns
- ↪IEnumerable<T>
- A new live view over the original enumerable that has
items
prepended to it.
Examples
const enumerable1 = IEnumerable.from([1, 2, 3, 4])
console.log(enumerable1.prepend(0).toArray()) // yields [0,1,2,3,4]
// more than one value can be used too
const enumerable2 = IEnumerable.from([3, 4, 5, 6])
console.log(enumerable2.prepend(1, 2).toArray()) // yields [1,2,3,4,5,6]
See Also
Overrides
Adds one or more elements to this list and returns the new length.
Remarks
Array.push()
function.Parameters
A map of options to pass to the method.
- values - T
- The values to append to the list.
Returns
- ↪number
- The new length of the list.
Defined in
reduce
<TAccumulate>(accumulator: function(TAccumulate, T, number, IEnumerable<T>):TAccumulate, initialValue: TAccumulate) : TAccumulateApplies the accumulator
function to this elements of this enumerable.
Type Parameters
- TAccumulate
- The type of the accumulated value.
Parameters
A map of options to pass to the method.
- accumulator - function(TAccumulate, T, number, IEnumerable<T>):TAccumulate
- A function which "adds" (accumulates) a value depending on the element and index to the initial value and returns the result.
Signature Details
function(total: TAccumulate, element: T, index: number, source: IEnumerable<T>) : TAccumulate
The accumulator to use for the IEnumerable<T>.A function which returns a result of typeTAccumulate
as an accumulation oftotal
andelement
.Parameters
- total - TAccumulate
- The previously returned value of the function.
- element - T
- The current element to accumulate.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- TAccumulate
- A result of type
TAccumulate
created from theelement
.
- initialValue - TAccumulate
- The initial value for the accumulator. Omit/use the reduce overload if you don't want to specify an initial value.
Returns
- ↪TAccumulate
- The result of the accumulation.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5])
const result = enumerable.reduce(
(accumulator, currentValue) => Math.max(accumulator, currentValue),
0,
)
console.log(result) // expected output: 5
See Also
Overrides
Applies the accumulator
function to this elements of this enumerable.
Parameters
A map of options to pass to the method.
- accumulator - function(T, T, number, IEnumerable<T>):T
- A function which "adds" (accumulates) a value depending on the element and index to the initial value and returns the result.
Signature Details
function(total: T, element: T, index: number, source: IEnumerable<T>) : T
The accumulator to use for the IEnumerable<T>.A function which returns a result of typeTAccumulate
as an accumulation oftotal
andelement
.Parameters
- total - T
- The previously returned value of the function.
- element - T
- The current element to accumulate.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- T
- A result of type
TAccumulate
created from theelement
.
Returns
- ↪T
- The result of the accumulation.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5])
const result = enumerable.reduce(
(accumulator, currentValue) => accumulator + currentValue,
)
console.log(result) // expected output: 15
See Also
Overrides
reduceRight
<TAccumulate>(accumulator: function(TAccumulate, T, number, IEnumerable<T>):TAccumulate, initialValue: TAccumulate) : TAccumulateApplies the accumulator
function to this elements of this enumerable in reverse order.
Type Parameters
- TAccumulate
- The type of the accumulated value.
Parameters
A map of options to pass to the method.
- accumulator - function(TAccumulate, T, number, IEnumerable<T>):TAccumulate
- A function which "adds" (accumulates) a value depending on the element and index to the seed value and returns the result.
Signature Details
function(total: TAccumulate, element: T, index: number, source: IEnumerable<T>) : TAccumulate
The accumulator to use for the IEnumerable<T>.A function which returns a result of typeTAccumulate
as an accumulation oftotal
andelement
.Parameters
- total - TAccumulate
- The previously returned value of the function.
- element - T
- The current element to accumulate.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- TAccumulate
- A result of type
TAccumulate
created from theelement
.
- initialValue - TAccumulate
- The initial value for the accumulator. Omit/use the reduceRight overload if you don't want to specify an initial value.
Returns
- ↪TAccumulate
- The final value of the accumulation.
Examples
const enumerable = IEnumerable.from(['a', 'b', 'c', 'd'])
const result = enumerable.reduceRight(
(accumulator, currentValue) => currentValue + accumulator,
'.',
)
console.log(result) // yields 'abcd.'
See Also
Overrides
Applies the accumulator
function to this elements of this enumerable in reverse order.
Parameters
A map of options to pass to the method.
- accumulator - function(T, T, number, IEnumerable<T>):T
- A function which "adds" (accumulates) a value depending on the element and index to the seed value and returns the result.
Signature Details
function(total: T, element: T, index: number, source: IEnumerable<T>) : T
The accumulator to use for the IEnumerable<T>.A function which returns a result of typeTAccumulate
as an accumulation oftotal
andelement
.Parameters
- total - T
- The previously returned value of the function.
- element - T
- The current element to accumulate.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- T
- A result of type
TAccumulate
created from theelement
.
Returns
- ↪T
- The result of the accumulation.
Examples
const enumerable = IEnumerable.from([
[0, 1],
[2, 3],
[4, 5],
])
const result = enumerable.reduceRight((accumulator, currentValue) =>
accumulator.concat(currentValue),
)
console.log(result) // yields [4,5,2,3,0,1]
See Also
Overrides
Removes the given element from this list.
Remarks
element
holds gets removed.Complexity
O(size())
Parameters
A map of options to pass to the method.
- element - T
- The element to be removed from the list.
Returns
- ↪boolean
true
if the element was found and removed;false
if the element was not found in the list.
Implements
Removes the given collection of objects from this list.
Parameters
A map of options to pass to the method.
- collection - IEnumerable<T>
- The collection of objects to be removed from the list.
Returns
- ↪boolean
- Whether there have been elements removed.
Removes the items from this list that match the predicate.
Parameters
A map of options to pass to the method.
- removeItemPredicate - function(T):boolean
- The predicate used to determine which items to remove from the list.
Signature Details
function(obj: T) : boolean
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.Parameters
- obj - T
- The object to compare against the criteria defined within the method represented by this delegate.
Returns
- boolean
true
if obj meets the criteria defined within the method represented by this delegate; otherwise,false
.
Returns
- ↪boolean
- Whether there have been elements removed.
Removes the item at the given index from the list.
Parameters
A map of options to pass to the method.
- index - number
- The index of the item to remove.
Implements
Removes the given list cell, and hence the element stored in it, from this list.
Complexity
O(1)
Preconditions
- The given list cell is part of this list.
Parameters
A map of options to pass to the method.
- c - ListCell<T>
- The list cell to be removed.
Returns
- ↪T
- The element that is stored in the removed cell.
Removes the first element from this list that matches the predicate.
Parameters
A map of options to pass to the method.
- removeItemPredicate - function(T):boolean
- The predicate used to determine which element to remove from the list. If
null
, the first element will be removed.Signature Details
function(obj: T) : boolean
Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.Parameters
- obj - T
- The object to compare against the criteria defined within the method represented by this delegate.
Returns
- boolean
true
if obj meets the criteria defined within the method represented by this delegate; otherwise,false
.
Returns
- ↪boolean
- Whether there have been elements removed.
Sets the element at the specified index.
Parameters
A map of options to pass to the method.
- index - number
- The zero-based index of the element to get or set.
- value - T
- The element at the specified index.
See Also
Implements
Removes and returns the first value in this list.
Remarks
Array.shift()
function and returns undefined
if the list is empty.Returns
- ↪T
- The removed value.
undefined
if the list was empty.
Defined in
Determines whether this enumerable contains any elements matching the given predicate
.
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- A function which returns
true
if the element matches a condition.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
- thisArg - any
- The optional object to use for
this
in thefunction
.
Returns
- ↪boolean
- Whether this enumerable contains any elements matching the given
predicate
.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5, 6, 7])
const result = enumerable.some((item) => item % 2 === 0)
console.log(result) // true
Overrides
Sorts the elements in this list according to the given comparison function.
Remarks
NOTE: The elements will be assigned to different list cells by this method.
The cells of the list will stay in the order but the info will be sorted.
Complexity
O(size() * log(size()))
Parameters
A map of options to pass to the method.
- comparator - function(T, T):number
- The comparison function to use for sorting the elements.
Signature Details
function(x: T, y: T) : number
Encapsulates a method that compares two objects.Parameters
- x - T
- The first object to compare.
- y - T
- The second object to compare.
Returns
- number
- An integer value which is
<0
ifx
is less thany
,0
ifx
is equal toy
, or>0
ifx
is greater thany
Transfers the contents of the given list to the end of this list.
Remarks
The given list will be empty after this operation.
Note that this operation transfers the list cells of the given list to this list. No new list cells are created by this operation.
Complexity
O(1)
Parameters
A map of options to pass to the method.
- list - YList<T>
- The list to transfer elements from.
Calculates the sum of the elements of this enumerable.
Parameters
A map of options to pass to the method.
- selector - function(T, number, IEnumerable<T>):number
- A function which returns a numeric value for the given element.
Signature Details
function(element: T, index: number, source: IEnumerable<T>) : number
The selector to use for the IEnumerable<T>.A function which returns a result of typeTResult
for eachelement
.Parameters
- element - T
- The current element create a result for.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- number
- A result of type
TResult
created from theelement
.
Returns
- ↪number
- The sum of the elements of the enumerable.
Examples
const enumerable = IEnumerable.from([
{ type: 'A', amount: 3 },
{ type: 'B', amount: 2 },
{ type: 'C', amount: 2 },
{ type: 'D', amount: 5 },
])
const result = enumerable.sum((element) => element.amount)
console.log(result) // yields 12
Overrides
Creates a dynamic view of this enumerable with the given number of elements taken from the start or end of this enumerable.
Parameters
A map of options to pass to the method.
- count - number
- The number of elements in the created enumerable. If the number is positive, the elements are taken from the beginning, otherwise, the elements are taken from the end.
Returns
- ↪IEnumerable<T>
- A dynamic iew of this enumerable with the given number of elements.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5, 6, 7])
// positive parameter means taking elements from the beginning of the enumerable
console.log(enumerable.take(2).toArray()) // yields [1,2]
// negative parameter means taking elements from the end of the enumerable
console.log(enumerable.take(-2).toArray()) // yields [6,7]
See Also
Overrides
Returns a dynamic IEnumerable<T> that contains the elements from this enumerable as long as the given predicate
is true
.
Remarks
Parameters
A map of options to pass to the method.
- predicate - function(T, number, IEnumerable<T>):boolean
- A function which returns
true
as long as the elements should be added to the returned enumerable.Signature Details
function(element: T, index: number, source: IEnumerable<T>) : boolean
The predicate to use for IEnumerable<T>.Parameters
- element - T
- The current element to test.
- index - number
- The element's index in the
- source - IEnumerable<T>
- The enumerable this function is called upon.
Returns
- boolean
true
if the item matches the condition.
Returns
- ↪IEnumerable<T>
- A dynamic enumerable that is a subset of the original enumerable.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 30, 4, 5])
const result = enumerable.takeWhile((item) => item < 5)
console.log(result.toArray()) // yields [1,2,3]
Overrides
Creates an array with the elements of this enumerable.
Creates a List<T> with the values of this enumerable.
Returns an dynamic enumerable which contains the elements of this enumerable in reverse order.
Remarks
Returns
- ↪IEnumerable<T>
- A dynamic enumerable which contains the elements of the original enumerable in reverse order.
Examples
const enumerable = IEnumerable.from([1, 2, 3, 4, 5])
const result = enumerable.toReversed()
console.log(result.toArray()) // yields [5,4,3,2,1]
const list = List.fromArray([1, 2, 3, 4, 5])
const result = list.toReversed()
list.add(0)
// result changed also
console.log(result.toArray()) // yields [0,5,4,3,2,1]
Overrides
Yields an ordered enumerable of the elements given an optional comparison function.
Remarks
Parameters
A map of options to pass to the method.
- comparator - function(T, T):number
- A function with the signature
(a, b) => number
which is used for the sort operation to compare the elements in the enumerable. May be omitted. If omitted, the elements are sorted by converting them to strings first. Numbers are sorted as numbers without converting them to strings (this is different to the EcmaScript implementation).Signature Details
function(value1: T, value2: T) : number
Compares the given values.Parameters
- value1 - T
- The first value to compare.
- value2 - T
- The second value to compare.
Returns
- number
- A value less than 0 if value 1 is smaller than value2, 0 if both values are equal, greater than 0 otherwise.
Returns
- ↪IEnumerable<T>
- A sorted enumeration of the original enumerable.
Examples
const enumerable = IEnumerable.from([
{ type: 'name-6', rank: 8 },
{ type: 'name-23', rank: 4 },
{ type: 'name-3', rank: 5 },
])
// order by the elements' rank
const sortedEnumerable1 = enumerable.toSorted(
(entryA, entryB) => entryA.rank - entryB.rank,
)
console.log(sortedEnumerable1.toArray())
// [{ "type": "name-23", "rank": 4 }, { "type": "name-3", "rank": 5 }, { "type": "name-6", "rank": 8 }]
// order by the elements' type using numeric collation to get a more "natural" order, with a comparer function
const collator = new Intl.Collator(undefined, {
numeric: true,
sensitivity: 'base',
})
const sortedEnumerable2 = enumerable.toSorted((entryA, entryB) =>
collator.compare(entryA.type, entryB.type),
)
console.log(sortedEnumerable2.toArray())
// [{ "type": "name-3", "rank": 5 }, { "type": "name-6", "rank": 8 },{ "type": "name-23", "rank": 4 }]
Overrides
Adds one or more elements to the beginning of this list and returns the new length.
Remarks
Array.unshift()
function.Parameters
A map of options to pass to the method.
- values - T
- The values to prepend to this list. The first argument will become the new first element in the list
Returns
- ↪number
- The new length of the list.
Defined in
Constants
An empty, read-only list.