Returns an optional that resolves to the element at the specified index
position in this stream. A negative index
counts from the last item (e.g., -1
refers to the last item, -2
to the second-to-last item, and so on). If the
specified position does not exist, the optional resolves to empty.
A zero-based position for the desired item, or a negative position to count from the end of the stream, converted to an integer.
An Optional containing the item at the specified position, or empty if no such position exists.
Calls Promise.all()
on the items in this stream and returns the resulting promise.
If the items in this stream are of type Promise<E>
, the result will be of type
Promise<E[]>
. For more complex cases, refer to the
MDN documentation.
A Promise
resolving to an array of results, where the type depends on
whether the items in the stream are promises.
Returns a stream containing all items of this stream, followed by the
provided items
.
The type of the items to append, defaults to T
-
the item type of this stream.
The items
to append to the end of the stream.
A Stream with the original items of this stream and
the appended items
.
Returns a stream containing all items of this stream, followed by all items
provided by the items
iterable.
The type of the items to append, defaults to T
-
the item type of this stream.
An iterable whose items will be appended to the end of this stream.
A Stream with the original items of this stream followed by the items from the provided iterable.
Returns a stream containing only the elements of this stream for which getKey
returns unique keys. If multiple items produce the same key, only the first such
item is included in the result stream.
The implementation uses a Set
under the hood, meaning that keys are compared
using the
SameValueZero
algorithm.
A function that extracts a key from each item to determine uniqueness. Receives an item and its index in the stream.
A Stream containing only the distinct items based on their keys.
Returns a stream containing all items except the first n
items of this stream.
If the stream contains n
or fewer items, the resulting stream will be empty.
The number of items to drop from the start of the stream, converted to an integer.
A Stream containing all items except the first n
.
Returns a stream containing all items except the last n
items of this stream.
If the stream contains n
or fewer items, the resulting stream will be empty.
The number of items to drop from the end of the stream, converted to an integer.
A Stream containing all items except the last n
.
Returns a stream containing all items of this stream after dropping the
trailing items that satisfy the given predicate
. Items are checked from
the end of the stream backward. Once an item does not match the predicate
,
all preceding items and that item are included in the resulting stream.
A function invoked for each item, with the following parameters:
- item
- The current item in the stream.
- index
- The index of the current item in the stream, starting from 0.
The function should return true
to drop the item or false
to stop dropping.
A Stream containing the remaining items after dropping the trailing
items that satisfy the predicate
.
Returns a stream containing all items of this stream after dropping the
leading items that satisfy the given predicate
. Once an item does not
match the predicate
, that item and all subsequent items are included in the
resulting stream.
A function invoked for each item, with the following parameters:
- item
- The current item in the stream.
- index
- The index of the current item in the stream, starting from 0.
The function should return true
to drop the item or false
to stop dropping.
A Stream containing the remaining items after dropping the leading
items that satisfy the predicate
.
Returns true
if the other
iterable contains the same number of items as this
stream and all corresponding items are equal using the ===
operator.
An iterable to test for equality with this stream.
true
if the iterables are equal in length and item-wise equality,
otherwise false
.
Returns true
if the predicate
returns true
for all items of this stream;
returns false
otherwise. For an empty stream, the result is true
.
This is a short-circuiting operation, meaning it stops processing items as soon
as the predicate
returns false
for any item.
A function to test each item in the stream. Receives an item and its index in the stream.
true
if all items satisfy the predicate or the stream is empty,
otherwise false
.
Returns a stream containing only the items of this stream that match the provided
predicate
.
A function to test each item in the stream. Receives an item and its index in the stream.
A Stream containing the items that satisfy the predicate.
Returns a stream with a possibly narrowed type, containing only the items of
this stream that match the provided predicate
. For example, this can be
used to filter out null
values from the stream.
streamOf('a', null, 'b')
.filter<string>(item => typeof item === 'string')
.toArray() satisfies string[]
A Stream containing only the items that satisfy the predicate.
Returns an Optional which resolves to the first item that matches the predicate, or to empty if no such item is found.
The function to test each item in the stream. Receives an item and its index in the stream.
An Optional that contains the first matching item or is empty if no item matches.
Returns an Optional containing the index of the first item in this
stream that satisfies the given predicate
. If no such item is found, the
optional resolves to empty.
A function that tests each item in the stream. It receives
the current item and its index as arguments and should return true
for the
desired item.
An Optional containing the index of the first matching item, or empty if no item matches the predicate.
Returns an Optional which resolves to the last item that matches the predicate, or to empty if no such item is found.
The function to test each item in the stream. Receives an item and its index in the stream.
An Optional that contains the last matching item or is empty if no item matches.
Returns an Optional containing the index of the last item in this
stream that satisfies the given predicate
. If no such item is found, the
optional resolves to empty.
A function that tests each item in the stream. It receives
the current item and its index as arguments and should return true
for the
desired item.
An Optional containing the index of the last matching item, or empty if no item matches the predicate.
Returns a stream with the following behavior:
depth
and included
in the resulting stream.Optional
depth: DThe depth of flattening. Defaults to 1.
A Stream containing the flattened items.
Returns a stream with the following behavior:
mapper
function to each item.mapper
function
into a single-level stream.This operation is also known as chain
.
A new Stream containing all items produced by the mapper
function, flattened into a single level.
Invokes the specified effect
function for each item in this stream until the
effect
function returns true
.
A function to apply to each item in the stream. The function can return a value that determines whether iteration should stop. Receives an item and its index in the stream.
true
if the iteration was stopped because the effect
function
returned true
, otherwise false
.
Returns a stream whose elements are [key, items[]]
pairs. Keys are
derived using the getKey
function applied to the items in this stream.
items
are arrays of stream elements that produced the same key
.
This method uses a Map
internally for grouping, meaning that keys are
compared using the
SameValueZero
algorithm.
The method works similar to Map.groupBy().
A Stream of [key, items[]]
pairs, where key
is a
derived key and items[]
is an array of elements associated with that key.
Collects the items of this stream into a Map.
Keys are generated using the provided getKey
function applied to each item in the stream.
Items that produce the same key (as determined by the Map
's
key equality)
are grouped into an array and stored as the value corresponding to that key.
The method works similar to Map.groupBy().
A Map
where each key is associated with an array of items that
produced the same key.
Concatenates the items of this stream, inserting sep
between them. Items are
coerced to strings using the
String function
(without new
).
Optional
sep: stringA string to insert between items. Defaults to ",".
A string representing the concatenated stream items.
Concatenates the items of this stream, inserting spec.sep
between them.
Additionally, can insert spec.leading
and spec.trailing
strings before and
after the result, if provided. Items are coerced to strings using the
String function
(without new
).
An object specifying:
sep
: A string to insert between items.leading
: A string to prepend to the result (optional).trailing
: A string to append to the result (optional).A string representing the concatenated stream items.
Like join but retrieves the separator by calling getSep
for each pair
of adjacent items.
@@param getSep - A function that returns the separator for each pair of adjacent items. Receives the left item, the right item, and the index of the left item in the stream.
A string representing the concatenated stream items with custom separators.
Returns a stream that executes an effect for each item, returning the item unchanged. This method can be used, for example, to mutate items in place.
The effect to execute on each item. Receives an item and its index in the stream.
A new Stream with the same items, after executing the effect.
Returns an optional that resolves to a random item from this stream, or resolves to empty if the stream is empty. The implementation uses the pseudo-random Math.random(), which means it's not suitable for cryptographic purposes.
An Optional containing a random item from the stream, or empty if the stream is empty.
Returns an optional with the following behavior:
reducer
function. The reducer
is first applied to the items at index 0 and index 1,
then to the result of the previous invocation and the item at the next index,
and so on.A function that reduces the stream's items. It receives the
following parameters:
- accumulator - The result of the previous reducer
invocation, or,
for the first invocation, the item at index 0.
- currentItem - The current item of the stream. For the first invocation,
the item at index 1.
- currentIndex - The index of the current item in the stream. For the
first invocation, this is 1.
An Optional containing the reduced value, or empty if the stream is empty.
If this stream is empty, returns initialValue
. Otherwise, applies reducer
to
initialValue
and the item at index 0, then applies reducer
to the previously
returned result and the item at index 1, and so on, returning the result from the
last reducer
invocation.
The function to reduce items. Takes the following parameters:
- accumulator - The result of the previous reducer
invocation, or,
for the first invocation, initialValue
.
- currentItem - The current item of the stream.
- currentIndex - The index of the current item in the stream. For the
first invocation, this is 0.
The initial value for the reduction process.
The result of the reduction process.
Returns an optional with the following behavior:
reducer
to the item at index size - 1
and the item at
index size - 2
, then applies reducer
to the previously returned result
and the item at index size - 3
, and so on, resolving to the value returned
by the final reducer
invocation.The function to reduce items. Takes the following parameters:
- accumulator - The result of the previous reducer
invocation, or,
for the first invocation, the item at index size - 1
.
- currentItem - The current item of the stream.
- currentIndex - The index of the current item in the stream, starting
from size - 2
and decreasing toward 0.
An Optional containing the result of the reduction, or empty if the stream is empty.
If this stream is empty, returns initialValue
. Otherwise, applies reducer
to
initialValue
and the item at the index size - 1
, then applies reducer
to the
previously returned result and the item at index size - 2
, and so on,
returning the result from the final reducer
invocation.
The function to reduce items. Takes the following parameters:
- accumulator - The result of the previous reducer
invocation, or,
for the first invocation, initialValue
.
- currentItem - The current item of the stream.
- currentIndex - The index of the current item in the stream, starting
from the index size - 1
and decreasing toward 0.
The initial value for the reduction process.
The result of the reduction process.
Returns a stream containing a portion of this stream, determined by the
start
and end
indices. The resulting stream includes the items starting
from the start
index up to, but not including, the end
index.
If no start
is specified, the slice begins at index 0
. If no end
is
specified, the slice ends at the last item in the stream. Negative indices
can be used for both start
and end
to indicate positions from the end of
the stream, where -1
refers to the last item, -2
refers to the
second-to-last item, and so on.
The method's behavior aligns with Array.slice().
Optional
start: numberThe zero-based index at which to begin the slice,
converted to an integer.
Defaults to 0
.
Optional
end: numberThe zero-based index before which to end the slice, converted to an integer. Defaults to the size of the stream.
A Stream containing the specified portion of this stream.
Returns true
if the predicate
returns true
for any item of this stream;
returns false
otherwise. This is a short-circuiting operation, meaning it
skips the remaining items once the predicate
returns true
for any item.
A function to test each item. Receives an item and its index in the stream.
true
if any item satisfies the predicate, false
otherwise.
Returns a stream with items of this stream ordered by the value returned by getComparable
.
The returned values are then compared with >
and <
. The implementation relies on
Array.sort().
A function that, given an item, returns a comparable value. The returned value is not cached, so it is recommended not to perform heavy computations in this function.
A Stream with items ordered by the comparable value.
Removes or replaces existing items in the stream and/or adds new items in their place. This method is similar to Array.prototype.splice, but instead of modifying the input in place and returning removed items, it leaves the input unchanged and returns a new stream reflecting the changes.
The type of the items to add, defaults to T
- the item type
of this stream.
The zero-based index at which to begin changing the stream,
converted to an integer.
A negative value counts from the end of the stream, with -1
referring to the last item.
start
exceeds the length of the stream, no items are deleted, and the provided
items
are added to the end of the stream.start
is less than the negative size of the stream, 0
is used.Optional
deleteCount: numberThe number of items to remove from the stream, converted to an integer.
Infinity
, all items from start
to the end of the stream are removed.0
, negative, or explicitly null
or undefined
, no items are removed.The items to add to the stream starting at start
. If no items are
provided, the method only removes items.
A stream containing the items after removing or replacing items.
Returns a stream whose items are groups of adjacent items from this stream for which
isSplit
returned false. In other words, given all items as a sequence, it splits them
between items for which isSplit
returns true
.
A Stream containing groups of adjacent items.
Returns a stream containing the trailing items of this stream that satisfy
the given predicate
. Once an item does not match the predicate
, this item
and all preceding items are excluded from the resulting stream.
A function invoked with each item and its index. Returns
true
to include the item in the resulting stream, or false
to stop
iterating.
A Stream containing the trailing items that satisfy the
given predicate
.
Returns a stream containing no more than n
random items from this stream.
The result is sampled without replacement,
meaning each item from this stream appears no more than once in the result.
The implementation uses pseudo-random Math.random(),
so it is not suitable for cryptographic purposes.
The maximum number of items to sample, converted to an integer.
A Stream containing up to n
random items.
Returns a stream containing the leading items of this stream that satisfy
the given predicate
. Once an item does not match the predicate
, this
item and all subsequent items are excluded from the resulting stream.
A function invoked with each item and its index. Returns
true
to include the item in the resulting stream, or false
to stop
iterating.
A Stream containing the leading items that satisfy the
given predicate
.
Converts a stream of [key, value]
pairs where key
is string
, number
, or symbol
,
to an object. If a key
appears multiple times in the stream, only the last
[key, value]
pair will be included in the result object. If the elements of the stream are
not [key, value]
pairs as described, an error will be thrown.
In TypeScript, you may need to use as const
after each tuple to help TypeScript infer the
correct tuple types:
stream(['a', 'b'] as const)
.map(key => [key, 0] as const)
.toObject() // => type is {a: 0, b: 0}
stream(['a', 'b'])
.map(key => [key, 0] as const)
.toObject() // => type is {[p: string]: 0}
stream(['a', 'b'])
.map(key => [key, 0])
.toObject() // => type is unknown
An object where the keys are from the stream's [key, value]
pairs and
the values are the corresponding values from those pairs.
Extension method to apply an arbitrary operator to the stream. An operator
is passed an iterable that yields this stream's items, and it must return an
iterator to be used in the next step. The easiest way to achieve this is by
using generators:
stream(1, 2, 3, 4, 5, 6)
.transform(function* (items) {
for (const item of items) {
if (item % 3 === 0) yield item;
}
}).toArray(); // => [3, 6]
A Stream based on the transformed items.
Like transform, but returns an optional that discards all items
except the first one yielded by the iterator returned by the operator
.
An Optional containing the first item yielded by the iterator, or empty if no items are yielded.
Returns a stream with the element at the specified index
replaced by the
provided value
, while all other elements remain unchanged.
Negative index counts back from the end of the array, with -1
being the last
element, -2
being the second to last, and so on. If the index is out of bounds,
a RangeError
is thrown.
The index of the element to replace, converted to an integer.
The value to insert at the specified index.
A Stream with the element at the specified index
replaced by
the provided value
.
Returns a stream whose elements are pairs, where the first element is an
item from this stream and the second element is the corresponding item
from the other
iterable. The length of the resulting stream is the
minimum of this stream's length and the other
iterable's length.
An iterable to zip with this stream.
A Stream of [T, U]
pairs.
Returns a stream composed of n
-tuples of consecutive elements from this
stream. If n
is greater than the number of items in the stream, the
resulting stream will be empty.
The number of consecutive elements to include in each tuple.
A Stream of n
-tuples of consecutive elements.
Similar to zip, but requires that this stream and the other
iterable have the same length. Throws an error if the lengths do not
match.
An iterable to zip with this stream.
A Stream of [T, U]
pairs.
Returns a stream of pairs, where each pair contains an item from this stream and the next item. The resulting stream has one less item than the original stream. If the original stream has only one element or is empty, the resulting stream will be empty.
A Stream of adjacent element pairs.
A stream is a thin wrapper over an iterable, providing intermediate and terminal operations to manipulate data. Like iterables, streams respect the order of items; unless explicitly specified otherwise, operations consume and transform items in their original order. Streams are lazy: all computations are triggered only during terminal operations.
A stream can be understood as a function over an iterable. Intermediate operations return new functions with the current stream as input, while terminal operations capture the input, evaluate the chain of functions, and return data.
Streams are stateless: they store only a reference to the input. This means streams can be iterated multiple times, and each time, the stream evaluates the input as it existed at the moment the terminal operation was invoked.
During evaluation, streams produce as little intermediate data as possible. For example, Stream.map does not copy input data; instead, it retrieves upstream data one by one and yields mapped items value by value. Some operations do require creating intermediate data structures, such as Stream.shuffle and Stream.sortBy. In such cases, downstream steps maximize the benefit of having a random-access array that can be modified.