class AsyncOfStream<T>
undocumented
Extends: AsyncStreamBase<T>
Type parameters
Name | Description |
---|---|
T | undocumented |
Properties
values
undocumented
values
Definition
readonly values:
ArrayNonEmpty
<AsyncOptLazy<T>>;
Methods
[Symbol.asyncIterator]
undocumented
[Symbol.asyncIterator]
append
Returns the current stream succeeded by the given value
append
value
Definition
append(value: AsyncOptLazy<T>):
AsyncStream.NonEmpty
<T>;
Parameters
Name | Type | Description |
---|---|---|
value | AsyncOptLazy<T> |
await AsyncStream.of(1, 2, 3).append(4).toArray()
// => [1, 2, 3, 4]
O(1)
Overrides
asNormal
undocumented
asNormal
assumeNonEmpty
Returns the stream as a non-empty instance.
assumeNonEmpty
Definition
assumeNonEmpty():
AsyncStream.NonEmpty
<T>;
RimbuError.EmptyCollectionAssumedNonEmptyError if the stream is known to be empty.
AsyncStream.from(Stream.range({ amount: 100 })).assumeNonEmpty()
// => type: AsyncStream.NonEmpty<number>
the function does not actually check if the stream is empty, so treat with extra care @note O(1)
Overrides
asyncStream
Returns an async stream of elements of type T.
asyncStream
collect
Returns an AsyncStream containing the resulting elements from applying the given collectFun
to each element in this Stream.
collect
collectFun
to each element in this Stream.Definition
collect<R>(collectFun:
AsyncCollectFun
<T, R>):
AsyncStream
<R>;
Type parameters
Name | Description |
---|---|
R |
Parameters
Name | Type | Description |
---|---|---|
collectFun | AsyncCollectFun <T, R> |
await AsyncStream.of(1, 2, 3).collect(async (v, i, skip, halt) => {
if (i === 0) return skip;
if (i === 1) halt();
return String(v)
}).toArray();
// => ['1']
O(1)
Overrides
concat
Returns an AsyncStream containing the elements of this stream followed by all elements produced by the others
array of AsyncStreamSources.
concat
others
array of AsyncStreamSources.Definition
concat(...others:
ArrayNonEmpty
<
AsyncStreamSource
<T>>): any;
Parameters
Name | Type | Description |
---|---|---|
others | ArrayNonEmpty < AsyncStreamSource <T>> |
await AsyncStream.of(1, 2, 3).concat([4, 5], () => [6, 7]).toArray()
// [1, 2, 3, 4, 5, 6, 7]
O(1)
Overrides
contains
Returns true if the AsyncStream contains given amount
instances of given value
, using given eq
function.
contains
amount
instances of given value
, using given eq
function.Definition
contains(searchValue: T, options?: {
amount?: number;
eq?: Eq<T>;
negate?: boolean;
}): Promise<boolean>;
Parameters
Name | Type | Description |
---|---|---|
searchValue | T | |
options | { amount?: number; eq?: Eq<T>; negate?: boolean; } |
const source = Stream.from('marmot')
await source.contains('m') // => true
await source.contains('m', 2) // => true
await source.contains('m', 3) // => false
await source.contains('q') // => false
O(N)
Overrides
containsSlice
Returns true if this stream contains the same sequence of elements as the given source
, false otherwise.
containsSlice
source
, false otherwise.Definition
containsSlice(source:
AsyncStreamSource.NonEmpty
<T>, options?: {
eq?: Eq<T>;
amount?: number;
}): Promise<boolean>;
Parameters
Name | Type | Description |
---|---|---|
source | AsyncStreamSource.NonEmpty <T> | |
options | { eq?: Eq<T>; amount?: number; } |
await AsyncStream.of(1, 2, 3, 4, 5).containsSlice([2, 3, 4])
// => true
await AsyncStream.of(1, 2, 3, 4, 5).containsSlice([4, 3, 2])
// => false
Overrides
count
Returns the amount of elements in the AsyncStream.
count
countElement
Returns the amount of elements that are equal according to the given eq
to the given value
in the AsyncStream.
countElement
eq
to the given value
in the AsyncStream.Definition
countElement(value: T, options?: {
eq?: Eq<T>;
negate?: boolean;
}): Promise<number>;
Parameters
Name | Type | Description |
---|---|---|
value | T | |
options | { eq?: Eq<T>; negate?: boolean; } |
await AsyncStream.of(1, 2, 3).countElement(2) // => 1
await AsyncStream.of(1, 2, 3).countElement(2, { negate: true }) // => 2
O(N) @note be careful not to use on infinite streams
Overrides
distinctPrevious
Returns an AsyncStream containing non-repetitive elements of the source stream, where repetitive elements are compared using the optionally given eq
equality function.
distinctPrevious
eq
equality function.Definition
distinctPrevious(options?: {
eq?: Eq<T>
|
undefined;
negate?: boolean
|
undefined;
}):
AsyncStream
<T>;
Parameters
Name | Type | Description |
---|---|---|
options | { eq?: Eq<T> | undefined; negate?: boolean | undefined; } |
await AsyncStream.of(1, 1, 2, 2, 3, 1).distinctPrevious().toArray()
// => [1, 2, 3, 1]
Overrides
AsyncStream.distinctPrevious, AsyncStreamBase.distinctPrevious
drop
Returns an AsyncStream that skips the first amount
elements of this stream and returns the rest.
drop
amount
elements of this stream and returns the rest.Definition
drop(amount: number):
AsyncStream
<T>;
Parameters
Name | Type | Description |
---|---|---|
amount | number |
await AsyncStream.of(1, 2, 3).drop(1).toArray() // => [2, 3]
O(N)
Overrides
dropWhile
Returns an AsyncStream that contains the elements of this stream starting from the first element that does not satisfy given pred
function.
dropWhile
pred
function.Definition
dropWhile(pred: (value: T, index: number) =>
MaybePromise
<boolean>, options?: {
negate?: boolean;
}):
AsyncStream
<T>;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number) => MaybePromise <boolean> | |
options | { negate?: boolean; } |
await AsyncStream.of(1, 2, 3).dropWhile(async v => v < 2).toArray()
// => [2, 3]
O(N)
Overrides
elementAt
Returns the element in the AsyncStream at the given index, or a fallback value (default undefined) otherwise.
elementAt
Definition
elementAt<O>(index: number, otherwise?: AsyncOptLazy<O>): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
index | number | |
otherwise | AsyncOptLazy<O> |
await AsyncStream.of(1, 2, 3).elementAt(1) // => 2
await AsyncStream.of(1, 2, 3).elementAt(5) // => undefined
await AsyncStream.of(1, 2, 3).elementAt(5, 'a') // => 'a'
await AsyncStream.of(1, 2, 3).elementAt(5, async () => 'a') // => 'a'
O(N) for most types of Stream
Overrides
equals
Returns true if the sequence of elements in this stream are equal to the sequence in the other
stream according to the provided eq
function.
equals
other
stream according to the provided eq
function.Definition
equals(other:
AsyncStreamSource
<T>, options?: {
eq?: Eq<T>;
negate?: boolean;
}): Promise<boolean>;
Parameters
Name | Type | Description |
---|---|---|
other | AsyncStreamSource <T> | |
options | { eq?: Eq<T>; negate?: boolean; } |
await AsyncStream.of(1, 2, 3).equals([1, 2, 3]) // => true
await AsyncStream.of(1, 2, 3, 4).equals([1, 2, 3]) // => false
don't use on potentially infinite streams @note O(N)
Overrides
every
Returns true if every element of the AsyncStream satifies given pred
function.
every
pred
function.Definition
every(pred: (value: T, index: number) =>
MaybePromise
<boolean>, options?: {
negate?: boolean;
}): Promise<boolean>;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number) => MaybePromise <boolean> | |
options | { negate?: boolean; } |
await AsyncStream.of(1, 2, 3).every((v, i) => v + i > 10) // => false
await AsyncStream.of(1, 2, 3).every(async (v, i) => v + i < 10) // => true
O(N)
Overrides
filter
Returns an AsyncStream containing only those elements from this stream for which the given pred
function returns true.
filter
pred
function returns true.Definition
filter(pred: (value: T, index: number, halt: () => void) =>
MaybePromise
<boolean>, options?: {
negate?: boolean
|
undefined;
}): any;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number, halt: () => void) => MaybePromise <boolean> | |
options | { negate?: boolean | undefined; } |
if the predicate is a type guard, the return type is automatically inferred @note O(1)
await AsyncStream.of(1, 2, 3).filter(async (v, i) => v + i !== 3).toArray()
// => [1, 3]
await AsyncStream.of(1, 2, 3).filter(async (v, i) => v + i !== 3, { negate: true }).toArray()
// => [2]
Overrides
filterPure
Returns an AsyncStream containing only those elements from this stream for which the given pred
function returns true.
filterPure
pred
function returns true.Definition
filterPure<A extends readonly unknown[]>(options: {
pred: (value: T, ...args: A) =>
MaybePromise
<boolean>;
negate?: boolean
|
undefined;
}, ...args: A): any;
Type parameters
Name | Constraints | Description |
---|---|---|
A | readonly unknown[] |
Parameters
Name | Type | Description |
---|---|---|
options | { pred: (value: T, ...args: A) => MaybePromise <boolean>; negate?: boolean | undefined; } | |
args | A |
if the predicate is a type guard, the return type is automatically inferred @note O(1)
await AsyncStream.of(1, 2, 3).filterPure({ pred: Object.is }, 2).toArray()
// => [2]
await AsyncStream.of(1, 2, 3).filterPure({ pred: Object.is, negate: true }, 2).toArray()
// => [1, 3]
Overrides
find
Returns the first element for which the given pred
function returns true, or a fallback value otherwise.
find
pred
function returns true, or a fallback value otherwise.Definition
find<O>(pred: (value: T, index: number) =>
MaybePromise
<boolean>, options?: {
occurrance?: number
|
undefined;
negate?: boolean
|
undefined;
otherwise?: AsyncOptLazy<O>;
}): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number) => MaybePromise <boolean> | |
options | { occurrance?: number | undefined; negate?: boolean | undefined; otherwise?: AsyncOptLazy<O>; } |
if the predicate is a type guard, the return type is automatically inferred
const isEven = async (v: number) => v % 2 === 0
await AsyncStream.of(1, 2, 3, 4).find(isEven) // => 2
await AsyncStream.of(1, 2, 3, 4).find(isEven, { occurrance: 2 }) // => 4
await AsyncStream.of(1, 2, 3, 4).find(isEven, { occurrance: 3 }) // => undefined
await AsyncStream.of(1, 2, 3, 4).find(isEven, { occurrance: 3, otherwise: 'a' })
// => 'a'
O(N)
Overrides
first
Returns the first element of the AsyncStream, or a fallback value (default undefined) if the stream is empty.
first
Definition
first<O>(otherwise?: AsyncOptLazy<O>): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
otherwise | AsyncOptLazy<O> |
await AsyncStream.of(1, 2, 3).first() // => 1
await AsyncStream.empty<number>().first() // => undefined
await AsyncStream.empty<number>().first(0) // => 0
await AsyncStream.empty<number>().first(async () => 0) // => 0
O(1)
Overrides
flatMap
Returns an AsyncStream consisting of the concatenation of flatMapFun
applied to each element.
flatMap
flatMapFun
applied to each element.Definition
flatMap<T2>(flatMapFun: (value: T, index: number, halt: () => void) =>
AsyncStreamSource
<T2>):
AsyncStream
<T2>;
Type parameters
Name | Description |
---|---|
T2 |
Parameters
Name | Type | Description |
---|---|---|
flatMapFun | (value: T, index: number, halt: () => void) => AsyncStreamSource <T2> |
O(1)
await AsyncStream.of(1, 2, 3).flatMap(async (v, i, halt) => {
if (i >= 1) halt();
return [v, i, v + i]
}).toArray()
// => [1, 0, 1, 2, 1, 3]
Overrides
flatZip
Returns an AsyncStream consisting of the concatenation of flatMapFun
applied to each element, zipped with the element that was provided to the function.
flatZip
flatMapFun
applied to each element, zipped with the element that was provided to the function.Definition
flatZip<T2>(flatMapFun: (value: T, index: number, halt: () => void) =>
AsyncStreamSource
<T2>):
AsyncStream
<[T, T2]>;
Type parameters
Name | Description |
---|---|
T2 |
Parameters
Name | Type | Description |
---|---|---|
flatMapFun | (value: T, index: number, halt: () => void) => AsyncStreamSource <T2> |
O(1)
await AsyncStream.of(1, 2, 3).flatZip((v, i, halt) => {
if (i >= 1) halt();
return [v, i, v + i]
}).toArray()
// => [[1, 1], [1, 0], [1, 1], [2, 2], [2, 1], [2, 3]]
Overrides
fold
Returns the value resulting from applying the given the given next
function to a current state (initially the given init
value), and the next stream value, and returning the new state. When all elements are processed, the resulting state is returned.
fold
next
function to a current state (initially the given init
value), and the next stream value, and returning the new state. When all elements are processed, the resulting state is returned.Definition
fold<R>(init: AsyncOptLazy<R>, next: (current: R, value: T, index: number, halt: () => void) =>
MaybePromise
<R>): Promise<R>;
Type parameters
Name | Description |
---|---|
R |
Parameters
Name | Type | Description |
---|---|---|
init | AsyncOptLazy<R> | |
next | (current: R, value: T, index: number, halt: () => void) => MaybePromise <R> |
console.log(await AsyncStream.empty<number>().fold(5, async (current, value) => current + value))
// => 5
console.log(await AsyncStream.of(1, 2, 3).fold(() => 5, (current, value) => current + value))
// => 11 (= 5 + 1 + 2 + 3)
Overrides
foldStream
Returns an AsyncStream containing the values resulting from applying the given the given next
function to a current state (initially the given init
value), and the next stream value, and returning the new state.
foldStream
next
function to a current state (initially the given init
value), and the next stream value, and returning the new state.Definition
foldStream<R>(init: AsyncOptLazy<R>, next: (current: R, value: T, index: number, halt: () => void) =>
MaybePromise
<R>):
AsyncStream
<R>;
Type parameters
Name | Description |
---|---|
R |
Parameters
Name | Type | Description |
---|---|---|
init | AsyncOptLazy<R> | |
next | (current: R, value: T, index: number, halt: () => void) => MaybePromise <R> |
console.log(
await AsyncStream.empty<number>()
.foldStream(5, async (current, value) => current + value)
.toArray()
)
// => []
console.log(
await AsyncStream.of(1, 2, 3)
.foldStream(() => 5, (current, value) => current + value)
.toArray()
)
// => [6, 8, 11]
Overrides
forEach
Performs given function f
for each element of the Stream, using given state
as initial traversal state.
forEach
f
for each element of the Stream, using given state
as initial traversal state.Definition
forEach(f: (value: T, index: number, halt: () => void) =>
MaybePromise
<void>, options?: {
state?:
TraverseState
;
}): Promise<void>;
Parameters
Name | Type | Description |
---|---|---|
f | (value: T, index: number, halt: () => void) => MaybePromise <void> | |
options | { state?: TraverseState ; } |
if f is an async function, each call will be awaited consecutively
await AsyncStream.of(1, 2, 3).forEach(async (v, i, halt) => {
console.log(v);
if (i >= 1) halt();
})
// => 1, 2
O(N)
Overrides
forEachPure
Performs given function f
for each element of the Stream, with the optionally given args
as extra arguments.
forEachPure
f
for each element of the Stream, with the optionally given args
as extra arguments.Definition
forEachPure<A extends readonly unknown[]>(f: (value: T, ...args: A) =>
MaybePromise
<void>, ...args: A): Promise<void>;
Type parameters
Name | Constraints | Description |
---|---|---|
A | readonly unknown[] |
Parameters
Name | Type | Description |
---|---|---|
f | (value: T, ...args: A) => MaybePromise <void> | |
args | A |
if f is an async function, each call will be awaited consecutively
await AsyncStream.of(1, 2, 3).forEachPure(console.log, 'sheep')
// => logs:
// 1 sheep
// 2 sheep
// 3 sheep
O(N)
Overrides
groupBy
Returns a promise resolving to the result of applying the valueToKey
function to calculate a key for each value, and feeding the tuple of the key and the value to the collector
reducer, and finally returning its result. If no collector is given, the default collector will return a JS multimap of the type Map<K, V[]>
.
groupBy
valueToKey
function to calculate a key for each value, and feeding the tuple of the key and the value to the collector
reducer, and finally returning its result. If no collector is given, the default collector will return a JS multimap of the type Map<K, V[]>
.Definition
groupBy<K, R>(valueToKey: (value: T, index: number) =>
MaybePromise
<K>, options?: {
collector?:
AsyncReducer.Accept
<readonly [K, T], R>
|
undefined;
}): Promise<R>;
Type parameters
Name | Description |
---|---|
K | |
R |
Parameters
Name | Type | Description |
---|---|---|
valueToKey | (value: T, index: number) => MaybePromise <K> | |
options | { collector?: AsyncReducer.Accept <readonly [K, T], R> | undefined; } |
await AsyncStream.of(1, 2, 3).groupBy((v) => v % 2)
// => Map {0 => [2], 1 => [1, 3]}
Overrides
indexed
Returns an AsyncStream where each element in this stream is paired with its index
indexed
Definition
indexed(options?: {
startIndex?: number;
}):
AsyncStream
<[number, T]>;
Parameters
Name | Type | Description |
---|---|---|
options | { startIndex?: number; } |
await AsyncStream.of(1, 2, 3).indexed().toArray()
// => [[0, 1], [1, 2], [2, 3]]
O(1)
Overrides
indexOf
Returns the index of the occurrance
instance of given searchValue
in the AsyncStream, using given eq
function, or undefined if no such value is found.
indexOf
occurrance
instance of given searchValue
in the AsyncStream, using given eq
function, or undefined if no such value is found.Definition
indexOf(searchValue: T, options?: {
occurrance?: number
|
undefined;
eq?: Eq<T>
|
undefined;
negate?: boolean
|
undefined;
}): Promise<number
|
undefined>;
Parameters
Name | Type | Description |
---|---|---|
searchValue | T | |
options | { occurrance?: number | undefined; eq?: Eq<T> | undefined; negate?: boolean | undefined; } |
const source = AsyncStream.from('marmot')
await source.indexOf('m') // => 0
await source.indexOf('m', { occurrance: 2 }) // => 3
await source.indexOf('m', { occurrance: 3 }) // => undefined
await source.indexOf('q') // => undefined
O(N)
Overrides
indexWhere
Returns the index of the given occurrance
instance of the element in the AsyncStream that satisfies given pred
function, or undefined if no such instance is found.
indexWhere
occurrance
instance of the element in the AsyncStream that satisfies given pred
function, or undefined if no such instance is found.Definition
indexWhere(pred: (value: T, index: number) =>
MaybePromise
<boolean>, options?: {
occurrance?: number;
negate?: boolean;
}): Promise<number
|
undefined>;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number) => MaybePromise <boolean> | |
options | { occurrance?: number; negate?: boolean; } |
await AsyncStream.of(1, 2, 3).indexWhere((v, i) => v + i > 2) // => 1
await AsyncStream.of(1, 2, 3).indexWhere(async (v, i) => v + i > 2, { occurrance: 2 }) // => 2
O(N)
Overrides
indicesOf
Returns an AsyncStream containing the indicies of the occurrance of the given searchValue
, according to given eq
function.
indicesOf
searchValue
, according to given eq
function.Definition
indicesOf(searchValue: T, options?: {
eq?: Eq<T>;
negate?: boolean;
}):
AsyncStream
<number>;
Parameters
Name | Type | Description |
---|---|---|
searchValue | T | |
options | { eq?: Eq<T>; negate?: boolean; } |
await AsyncStream.from('marmot').indicesOf('m').toArray()
// => [0, 3]
O(N)
Overrides
indicesWhere
Returns an AsyncStream containing the indices of the elements for which the given pred
function returns true.
indicesWhere
pred
function returns true.Definition
indicesWhere(pred: (value: T) =>
MaybePromise
<boolean>, options?: {
negate?: boolean;
}):
AsyncStream
<number>;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T) => MaybePromise <boolean> | |
options | { negate?: boolean; } |
await AsyncStream.of(1, 2, 3).indicesWhere((v, i) => v + i !== 3).toArray()
// => [0, 2]
O(N)
Overrides
intersperse
Returns an AsyncStream with all elements from the given sep
AsyncStreamSource between two elements of this stream.
intersperse
sep
AsyncStreamSource between two elements of this stream.Definition
intersperse(sep:
AsyncStreamSource
<T>):
AsyncStream
<T>;
Parameters
Name | Type | Description |
---|---|---|
sep | AsyncStreamSource <T> |
await AsyncStream.of(1, 2, 3).intersperse("ab").toArray()
// => [1, 'a', 'b', 2, 'a', 'b', 3]
O(1)
Overrides
join
Returns a string resulting from converting each element to string with options.valueToString
, interspersed with options.sep
, starting with options.start
and ending with options.end
.
join
options.valueToString
, interspersed with options.sep
, starting with options.start
and ending with options.end
.Definition
join({ sep, start, end, valueToString, ifEmpty, }?: {
sep?: string
|
undefined;
start?: string
|
undefined;
end?: string
|
undefined;
valueToString?: StringConstructor
|
undefined;
ifEmpty?: undefined;
}): Promise<string>;
Parameters
Name | Type | Description |
---|---|---|
{ sep, start, end, valueToString, ifEmpty, } | { sep?: string | undefined; start?: string | undefined; end?: string | undefined; valueToString?: StringConstructor | undefined; ifEmpty?: undefined; } |
await AsyncStream.of(1, 2, 3).join({ start: '<', sep: ', ', end: '>' })
// => '<1, 2, 3>'
O(N)
Overrides
last
Returns the last element of the AsyncStream, or a fallback value (default undefined) if the stream is empty.
last
Definition
last<O>(otherwise?: AsyncOptLazy<O>): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
otherwise | AsyncOptLazy<O> |
await AsyncStream.of(1, 2, 3).last() // => 3
await AsyncStream.empty<number>().last() // => undefined
await AsyncStream.empty<number>().last(0) // => 0
await AsyncStream.empty<number>().last(async () => 0) // => 0
O(N)
Overrides
map
Returns an AsyncStream where mapFun
is applied to each element.
map
mapFun
is applied to each element.Definition
map<T2>(mapFun: (value: T, index: number) =>
MaybePromise
<T2>):
AsyncStream
<T2>;
Type parameters
Name | Description |
---|---|
T2 |
Parameters
Name | Type | Description |
---|---|---|
mapFun | (value: T, index: number) => MaybePromise <T2> |
await AsyncStream.of(1, 2, 3).map(async (v, i) => `[${i}]: ${v}`).toArray()
// => ['[0]: 1', '[1]: 2', '[2]: 3']
O(1)
Overrides
mapPure
Returns an AsyncStream where the given mapFun
is applied to each value in the stream, with optionally as extra arguments the given args
.
mapPure
mapFun
is applied to each value in the stream, with optionally as extra arguments the given args
.Definition
mapPure<T2, A extends readonly unknown[]>(mapFun: (value: T, ...args: A) =>
MaybePromise
<T2>, ...args: A):
AsyncStream
<T2>;
Type parameters
Name | Constraints | Description |
---|---|---|
T2 | ||
A | readonly unknown[] |
Parameters
Name | Type | Description |
---|---|---|
mapFun | (value: T, ...args: A) => MaybePromise <T2> | |
args | A |
is mostly aimed to increase performance so that an extra function is not required @note can be used on function that really expect 1 argument, since the normal map will also pass more arguments
const s = AsyncStream.of({ a: 1 }, { a: 2, c: { d: true } })
const s2 = s.mapPure(JSON.stringify, ['a'], 5)
// when stream is evaluated, will call JSON.stringify on each stream element with the given extra arguments
console.log(await s2.toArray())
// => ["{\n \"a\": 1\n}", "{\n \"a\": 2\n}"]
Overrides
max
Returns the maximum element of the AsyncStream according to a default compare function, or the provided otherwise
fallback value if the stream is empty.
max
otherwise
fallback value if the stream is empty.Definition
max<O>(otherwise?: AsyncOptLazy<O>): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
otherwise | AsyncOptLazy<O> |
await AsyncStream.of(5, 1, 3).max() // => 5
await AsyncStream.empty<number>().max() // => undefined
await AsyncStream.empty<number>().max('a') // => 'a'
O(N)
Overrides
maxBy
Returns the maximum element of the AsyncStream according to the provided compare
function, or the provided otherwise
fallback value if the stream is empty.
maxBy
compare
function, or the provided otherwise
fallback value if the stream is empty.Definition
maxBy<O>(compare: (v1: T, v2: T) => number, otherwise?: AsyncOptLazy<O>): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
compare | (v1: T, v2: T) => number | |
otherwise | AsyncOptLazy<O> |
function compareLength(a: string, b: string): number { return b.length - a.length };
await AsyncStream.of('abc', 'a', 'ab').maxBy(compareLength) // => 'abc'
await AsyncStream.empty<string>().maxBy(compareLength) // => undefined
await AsyncStream.empty<string>().maxBy(compareLength, 'a') // => 'a'
O(N)
Overrides
min
Returns the mimimum element of the AsyncStream according to a default compare function, or the provided otherwise
fallback value if the stream is empty.
min
otherwise
fallback value if the stream is empty.Definition
min<O>(otherwise?: AsyncOptLazy<O>): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
otherwise | AsyncOptLazy<O> |
await AsyncStream.of(5, 1, 3).min() // => 1
await AsyncStream.empty<number>().min() // => undefined
await AsyncStream.empty<number>().min('a') // => 'a'
await AsyncStream.empty<number>().min(async () => 'a') // => 'a'
O(N)
Overrides
minBy
Returns the mimimum element of the AsyncStream according to the provided compare
function, or the provided otherwise
fallback value if the stream is empty.
minBy
compare
function, or the provided otherwise
fallback value if the stream is empty.Definition
minBy<O>(compare: (v1: T, v2: T) => number, otherwise?: AsyncOptLazy<O>): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
compare | (v1: T, v2: T) => number | |
otherwise | AsyncOptLazy<O> |
function compareLength(a: string, b: string): number { return b.length - a.length };
await AsyncStream.of('abc', 'a', 'ab').minBy(compareLength) // => 'a'
await AsyncStream.empty<string>().minBy(compareLength) // => undefined
await AsyncStream.empty<string>().minBy(compareLength, 'a') // => 'a'
O(N)
Overrides
mkGroup
Returns an AsyncStream starting with options.sep
, then returning the elements of this Stream interspersed with options.sep
, and ending with options.end
.
mkGroup
options.sep
, then returning the elements of this Stream interspersed with options.sep
, and ending with options.end
.Definition
mkGroup({ sep, start, end, }?: {
sep?:
AsyncStreamSource
<T>;
start?:
AsyncStreamSource
<T>;
end?:
AsyncStreamSource
<T>;
}): any;
Parameters
Name | Type | Description |
---|---|---|
{ sep, start, end, } | { sep?: AsyncStreamSource <T>; start?: AsyncStreamSource <T>; end?: AsyncStreamSource <T>; } |
await AsyncStream.of(1, 2, 3).mkGroup({ start: '<<', sep: '-', end: '>>' }).toArray()
// => ['<', '<', 1, '-', 2, '-', 3, '>', '>']
O(N)
Overrides
partition
Returns a promise resolving to a tuple of which the first element is the result of collecting the elements for which the given predicate
is true, and the second one the result of collecting the other elements. Own reducers can be provided as collectors, by default the values are collected into an array.
partition
predicate
is true, and the second one the result of collecting the other elements. Own reducers can be provided as collectors, by default the values are collected into an array.Definition
partition(pred: (value: T, index: number) =>
MaybePromise
<boolean>, options?: {
collectorTrue?: any;
collectorFalse?: any;
}): Promise<[any, any]>;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number) => MaybePromise <boolean> | |
options | { collectorTrue?: any; collectorFalse?: any; } |
if the predicate is a type guard, the return type is automatically inferred
Overrides
prepend
Returns the current stream preceded by the given value
prepend
value
Definition
prepend(value: AsyncOptLazy<T>):
AsyncStream.NonEmpty
<T>;
Parameters
Name | Type | Description |
---|---|---|
value | AsyncOptLazy<T> |
await AsyncStream.of(1, 2, 3).prepend(0).toArray()
// => [0, 1, 2, 3]
O(1)
Overrides
reduce
Applies the given (Async)Reducer
to each element in the AsyncStream, and returns the final result.
reduce
(Async)Reducer
to each element in the AsyncStream, and returns the final result.Definition
reduce<const S extends
AsyncReducer.CombineShape
<T>>(shape: S &
AsyncReducer.CombineShape
<T>): Promise<
AsyncReducer.CombineResult
<S>>;
Type parameters
Name | Constraints | Description |
---|---|---|
S | AsyncReducer.CombineShape <T> |
Parameters
Name | Type | Description |
---|---|---|
shape | S & AsyncReducer.CombineShape <T> |
console.log(await AsyncStream.of(1, 2, 4).reduce(Reducer.sum))
// => 7
console.log(await AsyncStream.of(1, 2, 4).reduce(Reducer.product))
// => 8
Overrides
reduceStream
Returns an AsyncStream where the given AsyncReducer
is applied to each element in the stream.
reduceStream
AsyncReducer
is applied to each element in the stream.Definition
reduceStream<const S extends
AsyncReducer.CombineShape
<T>>(shape: S &
AsyncReducer.CombineShape
<T>):
AsyncStream
<
AsyncReducer.CombineResult
<S>>;
Type parameters
Name | Constraints | Description |
---|---|---|
S | AsyncReducer.CombineShape <T> |
Parameters
Name | Type | Description |
---|---|---|
shape | S & AsyncReducer.CombineShape <T> |
console.log(
await AsyncStream.of(1, 2, 4)
.reduceStream(Reducer.sum)
.toArray()
)
// => [1, 3, 7]
console.log(
await AsyncStream.of(1, 2, 4)
.reduce(Reducer.product)
.toArray()
)
// => [1, 2, 8]
Overrides
repeat
Returns an AsyncStream that returns the elements from this stream given amount
of times.
repeat
amount
of times.Definition
repeat(amount?: number):
AsyncStream
<T>;
Parameters
Name | Type | Description |
---|---|---|
amount | number |
const source = AsyncStream.of(1, 2, 3)
source.repeat() // => AsyncStream(1, 2, 3, 1, 2, 3, 1, 2, ...)
await source.repeat(1).toArray() // => [1, 2, 3]
await source.repeat(3).toArray() // => [1, 2, 3, 1, 2, 3, 1, 2, 3]
await source.repeat(-3).toArray() // => [1, 2, 3]
amount = undefined means that the AsyncStream is repeated indefintely @note amount = 1 means that the Stream is not repeated @note amount < 1 will be normalized to amount = 1 @note O(1)
Overrides
single
Returns the first element of the Stream if it only has one element, or a fallback value if the Stream does not have exactly one value.
single
Definition
single<O>(otherwise?: AsyncOptLazy<O>): Promise<T
|
O>;
Type parameters
Name | Description |
---|---|
O |
Parameters
Name | Type | Description |
---|---|---|
otherwise | AsyncOptLazy<O> |
await AsyncStream.empty<number>().single() // => undefined
await AsyncStream.of(1, 2, 3).single() // => undefined
await AsyncStream.of(1).single() // => 1
await AsyncStream.of(1, 2, 3).single(0) // => 0
Overrides
some
Returns true if any element of the AsyncStream satifies given pred
function.
some
pred
function.Definition
some(pred: (value: T, index: number) =>
MaybePromise
<boolean>, options?: {
negate?: boolean;
}): Promise<boolean>;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number) => MaybePromise <boolean> | |
options | { negate?: boolean; } |
await AsyncStream.of(1, 2, 3).some((v, i) => v + i > 10) // => false
await AsyncStream.of(1, 2, 3).some(async (v, i) => v + i > 1) // => true
O(N)
Overrides
splitOn
Returns an AsyncStream of collections of stream elements, where each array is filled with elements of this stream up to the next element that equals given sepElem
according to the given eq
function.
splitOn
sepElem
according to the given eq
function.Definition
splitOn<R>(sepElem: T, options?: {
eq?: Eq<T>
|
undefined;
negate?: boolean
|
undefined;
collector?:
AsyncReducer.Accept
<T, R>
|
undefined;
}):
AsyncStream
<R>;
Type parameters
Name | Description |
---|---|
R |
Parameters
Name | Type | Description |
---|---|---|
sepElem | T | |
options | { eq?: Eq<T> | undefined; negate?: boolean | undefined; collector?: AsyncReducer.Accept <T, R> | undefined; } |
await AsyncStream.from('marmot').splitOn('m').toArray() // => [[], ['a', 'r'], ['o', 't']]
O(1)
Overrides
splitOnSlice
Returns an AsyncStream of collections of stream elements, where each array is filled with elements of this stream up to the next sequence of elements that matches given sepSeq
ordered elements with the given eq
function.
splitOnSlice
sepSeq
ordered elements with the given eq
function.Definition
splitOnSlice<R>(sepSlice:
AsyncStreamSource
<T>, options?: {
eq?: Eq<T>
|
undefined;
collector?:
AsyncReducer.Accept
<T, R>
|
undefined;
}):
AsyncStream
<R>;
Type parameters
Name | Description |
---|---|
R |
Parameters
Name | Type | Description |
---|---|---|
sepSlice | AsyncStreamSource <T> | |
options | { eq?: Eq<T> | undefined; collector?: AsyncReducer.Accept <T, R> | undefined; } |
await AsyncStream.from('marmalade').splitSeq('ma').toArray() // => [[], ['r'], ['l', 'a', 'd', 'e']]
O(1)
Overrides
splitWhere
Returns an AsyncStream of collections of stream elements, where each array is filled with elements of this stream up to the next element that satisfies give function pred
.
splitWhere
pred
.Definition
splitWhere<R>(pred: (value: T, index: number) =>
MaybePromise
<boolean>, options?: {
negate?: boolean
|
undefined;
collector?:
AsyncReducer.Accept
<T, R>
|
undefined;
}):
AsyncStream
<R>;
Type parameters
Name | Description |
---|---|
R |
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number) => MaybePromise <boolean> | |
options | { negate?: boolean | undefined; collector?: AsyncReducer.Accept <T, R> | undefined; } |
await AsyncStream.of(1, 2, 3, 4).splitWhere(async v => v == 3).toArray()
// => [[1, 2], [4]]
O(1)
Overrides
take
Returns an AsyncStream that contains the elements of this stream up to a maximum of amount
elements.
take
amount
elements.Definition
take(amount: number):
AsyncStream
<T>;
Parameters
Name | Type | Description |
---|---|---|
amount | number |
await AsyncStream.of(1, 2, 3).take(2).toArray() // => [1, 2]
O(N)
Overrides
takeWhile
Returns an AsyncStream that contains the elements of this stream up to the first element that does not satisfy given pred
function.
takeWhile
pred
function.Definition
takeWhile(pred: (value: T, index: number) =>
MaybePromise
<boolean>, options?: {
negate?: boolean;
}):
AsyncStream
<T>;
Parameters
Name | Type | Description |
---|---|---|
pred | (value: T, index: number) => MaybePromise <boolean> | |
options | { negate?: boolean; } |
await AsyncStream.of(1, 2, 3).takeWhile(async v => v < 3).toArray()
// => [1, 2]
O(N)
Overrides
toArray
Returns an Array containing all elements in the AsyncStream.
toArray
toJSON
Returns a JSON representation of the AsyncStream.
toJSON
toString
Returns a string representation of the AsyncStream.
toString
transform
Returns an AsyncStream consisting of the concatenation of AsyncStreamSource elements resulting from applying the given reducer
to each element.
transform
reducer
to each element.Definition
transform<R>(transformer:
AsyncTransformer.Accept
<T, R>):
AsyncStream
<R>;
Type parameters
Name | Description |
---|---|
R |
Parameters
Name | Type | Description |
---|---|---|
transformer | AsyncTransformer.Accept <T, R> |
O(1)
await AsyncStream.of(1, 2, 3, 4, 5, 6).transform(AsyncTransformer.window(3)).toArray()
// => [[1, 2, 3], [4, 5, 6]]
Overrides
window
Returns an AsyncStream containing windows
of windowSize
consecutive elements of the source stream, with each window starting skipAmount
elements after the previous one.
window
windows
of windowSize
consecutive elements of the source stream, with each window starting skipAmount
elements after the previous one.Definition
window<R>(windowSize: number, options?: {
skipAmount?: number
|
undefined;
collector?:
AsyncReducer.Accept
<T, R>
|
undefined;
}):
AsyncStream
<R>;
Type parameters
Name | Description |
---|---|
R |
Parameters
Name | Type | Description |
---|---|---|
windowSize | number | |
options | { skipAmount?: number | undefined; collector?: AsyncReducer.Accept <T, R> | undefined; } |
await Stream.of(1, 2, 3, 4, 5, 6, 7).window(3).toArray()
// => [[1, 2, 3], [4, 5, 6]]
await Stream.of(1, 2, 3, 4, 5).window(3, 1).toArray()
// => [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
await Stream.of(1, 2, 3, 4).window(2, 2, AsyncReducer.toJSSet()).toArray()
// => [Set(1, 2), Set(3, 4)]
Overrides
withOnly
Returns an AsyncStream containing only those elements that are in the given values
array.
withOnly
values
array.Definition
withOnly<F extends T>(values: F[]):
AsyncStream
<F>;
Type parameters
Name | Constraints | Description |
---|---|---|
F | T |
Parameters
Name | Type | Description |
---|---|---|
values | F[] |