Input elements type
Creates an iterator which yields items of this stream
Creates an optional which resolves to an element of this stream at index
position (negative index
counts
from last item) if there is such a position, or resolves to empty otherwise.
Zero-based position to return an item at, or negative position where -1 means last, -2 last-but-one etc.
Calls Promise.all()
on this stream and returns the result. If this stream items are Promise<E>
returns Promise<E[]>
; for more complex cases please refer to
MDN documentation.
Creates a stream containing all but last items of this stream. The result stream is empty if this stream is empty or contains one item.
Creates a stream whose items are all items of this stream followed by provided item
.
Item to append
Creates a stream whose items are all items of this stream followed by all items provided by items
iterable.
Items to append to this stream
Creates a stream containing only elements of this stream for which getKey
returns different keys.
If multiple items produce the same key, only the first such item is included to the returned stream.
Keys are checked using Set which means items are compared mostly in terms of ===
; there are exceptions though,
see MDN documentation.
Function to get key of items
Returns true if other
contains the same number of items and all corresponding items are equal
in terms of ===
.
Iterable to test for equality
Returns true
if the predicate
returns true
for all items of this stream; returns false otherwise. This is
short-circuiting operation which means it skips the rest items if the predicate
returned false
for some item.
A function to test each item
Creates a stream containing items of this stream which match the provided predicate
.
The predicate to test items
Like filter but allows narrowing output type. For example, if you want to filter out null
s,
streamOf<string | null>('a', null, 'b')
.filterWithAssertion(function(item): item is string {
return typeof item === 'string'
})
.toArray() // => ['a', 'b']: string[]
Makes sense only for TypeScript code; for JavaScript works exactly the same way as filter.
The assertion to test items
Creates an optional which resolves to the first item to match predicate, or to empty if no such item found.
The predicate to test items
Creates a stream with the following behavior:
mapper
to each itemmapper
The operation is also known as chain
.
The function to transform items
Invokes effect
for each item of this stream
An effect to apply to items
Invokes effect
for each item of this stream until effect
returns true
.
An effect to apply to items which also returns stop flag.
True if iteration was stopped because some effect returned true
, false otherwise.
Creates a stream whose elements are [key, items[]]
pairs; key
s are retrieved with getKey
applied
to this stream items; items
s are arrays of this stream items which produced the same key
. Order of items
is the same as in this stream.
Function to get item key with
Creates an optional which resolves to this stream first item if the stream has item(s), or resolves to empty otherwise.
Concatenates this stream items inserting sep
between. Items are mapped to string using
String function
(without new
).
A string to insert between items, default ",".
Like join but can insert sep
in the beginning and (or) in the end
Object specifying separator, and whether to insert it before the first and after the last items
Like join but retrieves separator calling getSep
for each adjacent items pair.
A function to get separator for adjacent items pair
Creates an optional resolving to the last item of this stream if it's nonempty, or resolving to empty otherwise.
Creates a stream whose items are all items of this stream transformed by mapper
.
A function to transform items
Creates a stream executing an effect for each item and returning the item as is. One possible usage for this method is mutating items in place.
The effect to execute
Creates an optional resolving to a random item of this stream, or resolving to emtpy if this stream is empty.
Creates an optional with the following behavior:
reducer
to the first and second items, then applies reducer
to previously returned result
and third item etc, and resolves to a value returned by the last reducer
invocation.The function to reduce items
If this stream is empty, returns initial; otherwise applies reducer
to initial
and the first item,
then applies reducer
to previously returned result and second item etc, and returns result from last
reducer
invocation.
The function to reduce items
Initial value
Creates an optional with the following behavior:
reducer
to the last and 2nd to end items, then applies reducer
to previously returned
result and 3rd to end item etc, and resolves to a value returned by the last reducer
invocation.The function to reduce items
If this stream is empty, returns initial; otherwise applies reducer
to initial
and the last item,
then applies reducer
to previously returned result and 2nd to end item etc, and returns result from last
reducer
invocation.
The function to reduce items
Initial value
Creates a stream whose elements are elements of this stream in the reversed order.
Creates a stream whose elements are elements of this stream with random order.
Creates an optional which resolves to an item if this stream contains only one item, or which resolves to empty otherwise.
Returns the number of items in this stream
Returns true
if the predicate
returns true
for any item of this stream; returns false otherwise. This is
short-circuiting operation which means it skips the rest items if the predicate
returned true
for some item.
A function to test each item
Creates a stream whose items are sorted items of this stream. The implementation relies on Array.sort().
Comparison function with semantics described in MDN
Creates a stream with items of this stream ordered by value returned by getComparable
. Returned values
are compared with >
and <
.
A function which, given an item, returns comparable value. Returned value of the function is not cached, so it's recommended to not to perform heavy computations.
Creates a stream whose items are groups of this stream adjacent items for which isSplit
returned false;
in other words, given all items as a sequence, splits it between items for which isSplit
returns true.
Order of items in arrays is the same as it is in this stream.
A function to check if items sequence should be split between l
and r
items
Creates a stream which contains all but first item of this stream. If this stream is empty or contains one item the returned stream is empty.
Creates a stream containing no more than n
first items of this stream
Max leading items to select from this stream
Creates a stream containing no more than n
last items of this stream
Max trailing items to select from this stream
Creates a stream containing no more than n
random items of this stream. The result is sampled
without replacement i.e. one item of this stream
appears in the result stream no more than once.
Max items to sample
Returns stream items as an array
If elements of this stream are [key, value]
pairs where key
is string
, number
, or symbol
, collects
stream items to an object; if some key
appears multiple times in this stream, only the last [key
, value
]
will be included in the result object. If elements of this stream are not [key, value]
as described above
throws an error.
Using in TypeScript may require putting as const
after each tuple to help TS infer 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
Extension method to apply arbitrary operator to the stream. An operator
is passed an iterable which yields
this stream items, and it must return the iterator which is an input for the next step. The easiest way is
to use 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 function which is passed an iterable yielding this stream items, and which is expected to return iterator for downstream step
Like transform but returns optional which discards all but first item yielded by an iterator
returned by an operator
.
A function to transform this stream items
Creates a stream whose elements are pairs where the first element is this stream item and the second element
is the corresponding item of the other
iterable. The length of the result stream is the minimum of
this stream length and other
length.
An iterable to zip this stream with
Creates s stream of triplets where the first element is this stream item, second element is its index, and third one is this stream size.
Generated using TypeDoc
A stream is a thin wrapper over iterable, providing intermediate and terminal operations to manipulate data. Like iterables, streams respect items order; if not specified explicitly, operations consume and transform items in the original order. Streams are lazy: all computations are triggered only on terminal operations.
Stream can be explained as a function over an iterable. Intermediate operations return new functions containing current stream as an input; terminal operations capture input, evaluate functions chain and return data.
stream(1, 2, 3, 4) // factory .filter(i => i%2===0) // intermediate .map(i => -i) // intermediate .toArray() // terminal => [-2, -4]
Streams are stateless: they don't store anything except reference to an input. This means streams may be iterated multiple times, and each time the stream is evaluated against input as it was on the moment of invoking terminal operation.
const input = [10, 20, 30] const s = stream(input) .map(i => i + 1) s.toArray() // => [11, 21, 31] input.push(40) s.toArray() // => [11, 21, 31, 41]
During evaluation streams produce as little intermediate data as possible. For example, map does not copy input data; instead, it retrieves upstream data one by one, and pushes mapped items downstream also value by value. Some operations do require creating intermediate data structures, for example shuffle and sortBy; in this case downstream steps maximize the benefit of having random-access array allowed for modifications.
stream(30, 20, 10, 0) .map(i => i + 1) // does not copy data, maps on evaluation .sortBy(i => i) // copies data to the new array and sorts it .at(2) // makes a[2] on array created upstream .get() // => 21