Collectors are methods that consume the Stream and produce a new value.
Factories are methods that create new Streams from existing values or iterables.
Transformers are methods that create a new Stream from the original Stream by transforming the elements in some way.
Protected
constructorProtected
Readonly
Internal
sequencerThe underlying Sequencer object that provides the elements of the Stream
Converts the Stream to an array. This will consume the Stream. If the Stream is infinite, this will produce an infinite loop.
An array of the elements in the Stream
const s = Stream.range(5);
console.log(s.toArray()); // [0, 1, 2, 3, 4]
Converts the stream to a Map. This will consume the Stream.
If the Stream is infinite, this will produce an infinite loop.
The Stream element type must be a tuple of key-value pairs. Since there is no explicit tuple type in JavaScript/TypeScript, Typescript tuples are expected, that is, an array of length 2, where the first element is the key and the second element is the value.
A Map constructed from the elements in the Stream
ArgTypeError If the Stream element type is not a tuple
This is only enforced in compile time if the Stream is typed properly. The runtime check is not done prior to consumption. so the error will be thrown during consumption. Use with caution with Stream.chain, as it enables chaining streams of different types.
ArgCountError if the number of arguments is not 0
const s = Stream.from([
["a", 1],
["b", 2],
["c", 3],
] as [string, number][]);
console.log(s.toMap()); // Map(3) { 'a' => 1, 'b' => 2, 'c' => 3 }
Converts the Stream to a Set. This will consume the Stream.
If the Stream is infinite, this will produce an infinite loop.
A Set constructed from the elements in the Stream
const s = Stream.range(5);
console.log(s.toSet()); // Set(5) { 0, 1, 2, 3, 4 }
Static
emptyStatic
fromCreate a Stream from an iterable.
A Stream of the elements in the iterable
const s = Stream.from([1, 2, 3, 4, 5]);
console.log(s.toArray()) // [1, 2, 3, 4, 5]
const s = Stream.from("hello");
console.log(s.toArray()); // ["h", "e", "l", "l", "o"]
const s = Stream.from(new Set([1, 2, 3, 4, 5]));
console.log(s.toArray()); // [1, 2, 3, 4, 5]
const s = Stream.from(new Map([["a", 1], ["b", 2], ["c", 3]]));
console.log(s.toArray()); // [["a", 1], ["b", 2], ["c", 3]]
const s = Stream.from({
*[Symbol.iterator]() {
yield 1; yield 2; yield 3;
}
});
console.log(s.toArray()); // [1, 2, 3]
function* gen() {
for (let i = 0; i < 5; i++) {
yield i;
}
}
const s = Stream.from(gen());
console.log(s.toArray()); // [0, 1, 2, 3, 4]
Static
iterateGenerates a Stream by applying a function to the previous value, starting with the initial value. The Stream will never be exhausted.
If try to consume the Stream, it will run forever. You can use the Stream.take method to limit the number of elements.
The initial value
The function to apply to the previous value to get the next value
A Stream of the generated values
fn
is not a functionfor(const x of Stream.iterate(1, (x) => x * 2)) {
console.log(x);
if (x > 100) break;
}
// Output: 1 2 4 8 16 32 64 128
Static
ofCreates a stream from the variadic arguments.
Rest
...values: T[]The values to create the Stream from
A Stream of the values
const s = Stream.of(1, 2, 3, 4, 5);
console.log(s.toArray()); // [1, 2, 3, 4, 5]
const s = Stream.of(..."hello");
console.log(s.toArray()); // ["h", "e", "l", "l", "o"]
Static
rangeThe end of the range
A Stream of numbers from 0 to stop
(exclusive)
stop
is not a numberconst s = Stream.range(5);
console.log(s.toArray()); // [0, 1, 2, 3, 4]
The start of the range
The end of the range
Optional
step: numberThe step between each number in the range
A Stream of numbers from start
to stop
(exclusive) with a step of step
,
if step is positive.
A Stream of numbers from stop
down to start
(exclusive) with a step of step
,
if step is negative.
Empty Stream if start
and stop
are equal.
step
is zerostart
, stop
or step
are not numbersconst s = Stream.range(1, 6);
console.log(s.toArray()); // [1, 2, 3, 4, 5]
const s = Stream.range(1, 6, 2);
console.log(s.toArray()); // [1, 3, 5]
const s = Stream.range(5, 0, -1);
console.log(s.toArray()); // [5, 4, 3, 2, 1]
const s = Stream.range(5, 0);
console.log(s.toArray()); // []
Static
repeatRepeats a value indefinitely. The Stream will never be exhausted. If try to consume the Stream, it will run forever. You can use the Stream.take method to limit the number of elements.
The value to repeat
A Stream of the repeated value
for(const x of Stream.repeat(1)) {
console.log(x);
}
// Output: 1 1 1 1 1 ...
const s = Stream.repeat(5).take(5);
console.log(s.toArray()); // [5, 5, 5, 5, 5]
n
of elements first elements of the StreamStatic
zipCreates a new Stream by zipping together the elements of the iterables.
The resulting Stream will contain tuples of the elements of the original iterables. The resulting Stream will have the length of the shortest iterable.
Rest
...iterables: {Iterables to zip together
A new Stream containing tuples of the elements of the original iterables
const s1 = Stream.range(5);
const s2 = Stream.range(5, 10);
const s = Stream.zip(s1, s2);
console.log(s.toArray()); // [[0, 5], [1, 6], [2, 7], [3, 8], [4, 9]]
Checks if all the elements in the Stream are true
.
It is a short-circuiting operation, meaning that it will stop
as soon as it finds a false
element.
true
if the Stream is emptytrue
if all the elements in the Stream are true
, false
otherwiseconst s = Stream.range(5)
.filter(x => x % 2 === 0)
.map(x => x % 2 === 0);
console.log(s.all()); // true
Checks if all the elements in the Stream satisfy the predicate. This is similar to Stream.all, but it uses a predicate to check the elements. It is a short-circuiting operation, meaning that it will stop as soon as it finds an element that does not satisfy the predicate.
This operation is equivalent to calling Stream.map with and then Stream.all.
true
if the Stream is emptytrue
if all the elements in the Stream satisfy the predicate, false
otherwisefn
is not a functionconst s = Stream.range(5).filter(x => x % 2 === 0);
console.log(s.allMap(x => x % 2 === 0)); // true
Checks if any of the elements in the Stream are true
.
It is a short-circuiting operation, meaning that it will stop
as soon as it finds a true
element.
false
if the Stream is emptyif any of the elements in the Stream are
true,
false` otherwiseconst s = Stream.range(5)
.filter(x => x % 2 === 0)
.map(x => x % 2 === 0);
console.log(s.any()); // true
Checks if any of the elements in the Stream satisfy the predicate. This is similar to Stream.any, but it uses a predicate to check the elements. It is a short-circuiting operation, meaning that it will stop as soon as it finds an element that satisfies the predicate.
This operation is equivalent to calling Stream.map with and then Stream.any.
false
if the Stream is emptytrue
if any of the elements in the Stream satisfy the predicate, false
otherwisefn
is not a functionconst s = Stream.range(5).filter(x => x % 2 === 0);
console.log(s.anyMap(x => x % 2 === 0)); // false
Compares the elements of the Stream with the elements of another Stream. The comparison is done lexicographically, that is, the first elements of each Stream are compared, then the second elements, and so on.
other
is not a Streamconst s1 = Stream.range(5);
const s2 = Stream.range(5);
console.log(s1.compare(s2)); // 0
const s1 = Stream.from("aab");
const s2 = Stream.from("abb");
console.log(s1.compare(s2)); // -1
Compares the elements of the Stream with the elements of another Stream using a key function to map the elements to a comparable value.
Behaves like Stream.compare, but uses a key function to map the elements to a comparable value.
other
is not a Streamfn
is not a functionconst s1 = Stream.from(["a", "aa", "aaa"]);
const s2 = Stream.from(["aa", "aaa", "aaaa"]);
console.log(s1.compareBy(s2, x => x.length)); // -1
Counts the elements in the Stream.
The number of elements in the Stream
const s = Stream.range(5);
console.log(s.count()); // 5
const s = Stream.range(5).filter(x => x % 2 === 0);
console.log(s.count()); // 3
Checks if the elements of the Stream are equal to the elements of another Stream.
true
if the Streams are equal, false
otherwise
other
is not a Streamconst s1 = Stream.range(5);
const s2 = Stream.range(5);
console.log(s1.eq(s2)); // true
Checks if the elements of the Stream are equal to the elements of another Stream by comparing the elements using a key function.
true
if the Streams are equal, false
otherwise
other
is not a Streamfn
is not a functionconst s1 = Stream.from(["a", "aa", "aaa"]);
const s2 = Stream.from(["aa", "aaa", "aaaa"]);
console.log(s1.eqBy(s2, x => x.length)); // false
Finds the first element in the Stream that satisfies the predicate.
If no element satisfies the predicate, returns undefined
.
The function to apply to each element in the Stream
The first element in the Stream that satisfies the predicate,
or undefined
if no element satisfies the predicate
predicate
is not a functionconst isPrime = require('is-prime-number'); // npm install is-prime-number
const x = Stream.range(20, Infinity).findFirst(isPrime);
console.log(x); // 23
is-prime-number
- prime number checker libraryFinds the last element in the Stream that satisfies the predicate.
If no element satisfies the predicate, returns undefined
.
The function to apply to each element in the Stream
The last element in the Stream that satisfies the predicate,
or undefined
if no element satisfies the predicate
predicate
is not a functionconst isPrime = require('is-prime-number'); // npm install is-prime-number
const x = Stream.range(20).findLast(isPrime);
console.log(x); // 19
Performs a reduction on the elements of the Stream using the fn
function.
The initial value of the accumulator
The function to apply to each element in the Stream and the accumulator. For each element, the function is called with the accumulator and the element as arguments.
The result of the reduction
fn
is not a functionThis is a terminal operation, meaning that it consumes the Stream. If the Stream is infinite, this will produce an infinite loop.
const s = Stream.range(5);
console.log(s.fold(0, (acc, x) => acc + x)); // 10
const s = Stream.range(1, 6);
console.log(s.fold(1, (acc, x) => acc * x)); // 120
const s = Stream.from([["a", 1], ["b", 2], ["c", 3]]);
console.log(s.fold({}, (acc, [k, v]) => ({...acc, [k]: v}))); // { a: 1, b: 2, c: 3 }
Calls a function for each element in the Stream.
fn
is not a functionconst s = Stream.range(5);
s.forEach(console.log);
// Output:
// 0
// 1
// 2
// 3
// 4
Checks if the elements of the Stream are greater than or equal to the elements of another Stream
true
if the Streams are greater than or equal, false
otherwise
other
is not a StreamSimilar operations
Checks if the elements of the Stream are greater than the elements of another Stream
true
if the Streams are greater, false
otherwise
other
is not a StreamSimilar operations
Checks if the elements of the Stream are sorted in ascending order.
true
if the elements are sorted, false
otherwise
key
is not a functionconst s = Stream.range(5);
console.log(s.isSorted()); // true
const s = Stream.from(["a", "aa", "aaa"]);
console.log(s.isSorted(false, s=> s.length)); // true
const s = Stream.range(5, 0, -1);
console.log(s.isSorted(true)); // true
Checks if the elements of the Stream are sorted in ascending order.
A function to compare the elements
If true, checks if the elements are sorted in descending order
true
if the elements are sorted, false
otherwise
fn
is not a functionconst s = Stream.from(["a", "aa", "aaa"]);
console.log(s.isSortedBy((s1, s2) => s1.length - s2.length)); // true
Joins the elements of the Stream into a single string,
similar to the Array.prototype.join
method.
The separator to use between the elements
A string containing the elements of the Stream
separator
is not a stringconst s = Stream.from(["a", "b", "c"]);
console.log(s.join()); // "abc"
const s = Stream.from(["a", "b", "c"]);
console.log(s.join("-")); // "a-b-c"
Checks if the elements of the Stream are less than or equal to the elements of another Stream
true
if the Streams are less than or equal, false
otherwise
other
is not a StreamSimilar operations
Checks if the elements of the Stream are not equal to the elements of another Stream
true
if the Streams are not equal, false
otherwise
other
is not a Streamfn
is not a functionFinds the maximum element in the Stream.
The maximum element in the Stream
const s = Stream.range(5);
console.log(s.max()); // 4
Finds the minimum element in the Stream.
The minimum element in the Stream
const s = Stream.range(5);
console.log(s.min()); // 0
Checks if the elements of the Stream are not equal to the elements of another Stream.
true
if the Streams are not equal, false
otherwise
other
is not a Streamconst s1 = Stream.range(5);
const s2 = Stream.range(5);
console.log(s1.ne(s2)); // false
Multiplies the elements of the Stream of numbers.
const s = Stream.range(1, 6);
console.log(s.product()); // 120
Reduction operation that reduces the elements of the Stream to a single value. It is similar to Stream.fold, but it does not require an initial value. Instead, it uses the first element of the Stream as the initial value.
The function to apply to each element in the Stream and the accumulator. For each element, the function is called with the accumulator and the element as arguments.
undefined
if the Stream is emptyfn
is not a functionconst s = Stream.range(5);
console.log(s.reduce((acc, x) => acc + x)); // 10
function factorial(n: number) {
return Stream.range(n, 0, -1).reduce((acc, x) => acc * x);
}
console.log(factorial(5)); // 120
Adds the elements of the Stream of numbers.
const s = Stream.range(1, 20, 2);
console.log(s.sum()); // 100
Chains the elements of the Stream with the elements of another iterable. The resulting Stream will contain all the elements of the original Stream, followed by the elements of the other iterable.
The other iterable to chain with
A new Stream containing all the elements of the original Stream, and the elements of the other iterable
iterable
is not an iterableconst s = Stream.range(3).chain([3, 4, 5]);
console.log(s.toArray()); // [0, 1, 2, 3, 4, 5]
Creates a new stream by chunking the elements of the original Stream into chunks of size n
.
The resulting Stream will contain arrays of the elements of the original Stream.
The last chunk may contain fewer than n
elements.
A new Stream containing arrays of the elements of the original Stream
n
is less than or equal to zeron
is not a numberconst s = Stream.range(10).chunk(3);
console.log(s.toArray()); // [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9]]
const s = Stream.range(9).chunk(2);
console.log(s.toArray()); // [[0, 1], [2, 3], [4, 5], [6, 7], [8]]
@ see
Enumerates the elements in the Stream. This is useful for getting the index of each element.
Transforms the Stream of elements into a Stream of tuples, where the first element of the tuple is the index
A new Stream of tuples, where the first element of the tuple is the index
const s = Stream.from('abc').enumerate();
console.log(s.toArray()); // [[0, "a"], [1, "b"], [2, "c"]]
Creates a new Stream containing only the elements of the original Stream that satisfy the predicate.
A new Stream where each element of type U
predicate
is not a functionconst s = Stream.range(5).filter(x => x % 2 === 0);
console.log(s.toArray()); // [0, 2, 4]
Flattens a Stream of iterables into a single Stream. This is useful for flattening a Stream of Streams, or a Stream of arrays. The resulting Stream will contain all the elements of the original Stream of iterables. The order of the elements is preserved.
A new Stream containing all the elements of the original Stream of iterables
const s = Stream.from([[1, 2], [3, 4], [5, 6]]);
console.log(s.flatten().toArray()); // [1, 2, 3, 4, 5, 6]
const s = Stream.range(1, 4).map((x) => Stream.range(x));
console.log(s.flatten().toArray()); // [0, 0, 1, 0, 1, 2]
Creates a new Stream where each element is the result of
applying the function fn
to each element in the original Stream.
A new Stream where each element of type U
fn
is not a functionconst s = Stream.range(5).map(x => x * 2);
console.log(s.toArray()); // [0, 2, 4, 6, 8]
const s = Stream.from("hello").map(x => x.toUpperCase());
console.log(s.toArray()); // ["H", "E", "L", "L", "O"]
Creates a new Stream with the first n
elements of the original Stream skipped.
If the number of elements in the Stream is less than n
, the new Stream will be empty.
The number of elements to skip
A new Stream with the first n
elements of the original Stream skipped.
n
is less than zeron
is not a numberconst s = Stream.range(5).skip(3);
console.log(s.toArray()); // [3, 4]
n
elements of the StreamCreates a new Stream by sliding a window of size n
over the elements of the original Stream,
with a step of step
. The resulting Stream will contain arrays of the elements of the original Stream.
A new Stream containing arrays of the elements of the original Stream
n
is not a numbern
is less than or equal to zeroconst s = Stream.range(5).slide(3);
console.log(s.toArray()); // [[0, 1, 2], [1, 2, 3], [2, 3, 4]]
Creates a new Stream with the first n
elements of the original Stream.
The number of elements to take
A new Stream with the first n
elements of the original Stream.
n
is less than zeroconst s = Stream.range(5).take(3);
console.log(s.toArray()); // [0, 1, 2]
n
elements of the StreamCreates a new Stream that contains all the elements of the original Stream, before the first element that does not satisfy the predicate.
A new Stream that contains all the elements of the original Stream,
predicate
is not a functionconst s = Stream.range(5).takeWhile(x => x < 3);
console.log(s.toArray()); // [0, 1, 2]
n
elements of the StreamGenerated using TypeDoc
A Stream is a sequence of values that can be manipulated using a variety of methods. Streams are created from iterables, arrays, or using the
range
,repeat
and other factory methods. Streams are lazy, meaning that they do not evaluate their elements until they are consumed by a method that requires it, so-called terminal operations. This allows for efficient processing of large or infinite sequences