Operators
catchError
▸ catchError<T
, TAlternative
>(action
): Operator
<T
, T
| TAlternative
>
If the original iterable sequence raises an exception, the specified action is performed, terminating the enumeration or enumerating an alternate sequence.
const result = from([1,2,3]).pipe(
tap(i => {
if(i === 2) throw new Error('test');
}),
catchError((e) => {
if(e instanceof Error) {
console.log(`error occurred: ${e.message}`)
}
})
).toArray();
//result: [1]
//console: error occurred: test
const result = from([1,2,3]).pipe(
tap(i => {
if(i === 2) throw new Error();
}),
catchError(() => [4,5,6])
).toArray();
//result: [1,4,5,6]
with nested & finalize
const alternative = from([4,5,6]).pipe(
tap(i => {
if(i === 5) throw new Error();
}),
catchError(() => [7,8,9]),
finalize(() => console.log('seq 2 finished')),
)
const result = from([1,2,3]).pipe(
tap(i => {
if(i === 2) throw new Error();
}),
catchError(() => alternative),
finalize(() => console.log('seq 1 finished'))
).toArray();
//result: [1,4,7,8,9]
//console: seq 2 finished
//console: seq 1 finished
different type
const output = from([1,2,3]).pipe(
tap(i => {
if(i === 2) throw new Error();
}),
catchError(() => ['a', 'b', 'c'])
).toArray();
// type output : (number | string)[]
// output : [1, 'a', 'b', 'c']
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TAlternative | T | alternative iterable element type. |
Parameters
Name | Type | Description |
---|---|---|
action | (error : unknown ) => void | (error : unknown ) => Iterable <TAlternative > | finalize action. Returns void or an alternative Iterable. |
Returns
Operator
<T
, T
| TAlternative
>
Operator function.
Defined in
chunk
▸ chunk<T
>(size
): Operator
<T
, readonly T
[]>
Returns a sequence divided into array of the specified size.
const result = from([1, 2, 3, 4, 5, 6, 7]).pipe(chunk(2)).toArray();
//result: [[1,2],[3,4],[5,6],[7]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
size | number | Length of elements per array. |
Returns
Operator
<T
, readonly T
[]>
Operator function.
Defined in
chunkByAccumulation
▸ chunkByAccumulation<T
, TAccumulate
>(seed
, func
, thresholdUntil
): Operator
<T
, readonly T
[]>
Returns a sequence divided into arrays based on an accumulation function and a threshold condition.
const result1 = from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).pipe(
chunkByAccumulation(
0,
(acc, current) => acc + current,
(acc) => acc <= 10
)
).toArray();
// result1: [[1, 2, 3, 4], [5], [6], [7], [8], [9], [10]]
// Example where the first value doesn't satisfy the condition
const result2 = from([11, 1, 2, 3]).pipe(
chunkByAccumulation(
0,
(acc, current) => acc + current,
(acc) => acc <= 10
)
).toArray();
// result2: []
// Example where enumeration stops when a single value doesn't satisfy the condition
const result3 = from([1, 2, 3, 15, 4, 5]).pipe(
chunkByAccumulation(
0,
(acc, current) => acc + current,
(acc) => acc <= 10
)
).toArray();
// result3: [[1, 2, 3]]
In these examples:
- A new chunk starts whenever the sum exceeds 10.
- In result2, an empty array is returned because the first value (11) immediately fails the condition.
- In result3, enumeration stops at 15 because it fails the condition on its own, and the previous chunk is returned.
Remarks
- The accumulation is reset to the seed value at the beginning of each new chunk.
- If the first value in the sequence doesn't satisfy the thresholdUntil condition, an empty sequence is returned.
- If at any point in the sequence a single value doesn't satisfy the thresholdUntil condition, the enumeration stops at that point.
Type parameters
Name | Description |
---|---|
T | Source element type. |
TAccumulate | The type returned by the aggregate function. |
Parameters
Name | Type | Description |
---|---|---|
seed | TAccumulate | The initial value for the accumulation. |
func | (accInChunk : TAccumulate , current : T ) => TAccumulate | A function that takes the current accumulation and the current element, and returns a new accumulation. |
thresholdUntil | (accInChunk : TAccumulate ) => boolean | A function that takes the current accumulation and returns true if the chunk should continue, or false if a new chunk should start. When this function returns false, the accumulation is reset to the seed value for the next chunk. |
Returns
Operator
<T
, readonly T
[]>
Operator function.
Defined in
concat
▸ concat<T
>(target
): Operator
<T
, T
>
Returns a sequence in which the current sequence and the specified sequence are concatenated.
const result = from([1, 2]).pipe(concat([3,4])).toArray();
//result: [1,2,3,4]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
target | Iterable <T > | Sequence to be concatenated. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
concatValue
▸ concatValue<T
>(target
): Operator
<T
, T
>
Returns the sequence to which the specified value is added.
const result = from([1, 2]).pipe(concatValue(3)).toArray();
//result: [1,2,3]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
target | T | Element you want to add to the sequence. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
difference
▸ difference<T
, TComparableValue
, TKey
>(target
, keySelector?
, removeDuplicate?
, comparableValueForKey?
): Operator
<T
, T
>
Returns the sequence that is the difference set between the current sequence and the specified sequence.
const result1 = from([1, 2, 3, 6, 6]).pipe(difference([2,3,4,5])).toArray();
//result1: [1,6]
const result2 = from([1, 2, 3, 6, 6]).pipe(difference([2,3,4,5],i => i, false)).toArray();
//result2: [1,6,6]
const source = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'b'}, value: "test2"},
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test3"},
{groupKey: {mainKey: 1, subKey: 'c'}, value: "test4"},
];
const target = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'e'}, value: "test2"},
];
const result3 = from(source).pipe(
difference(
target,
i => i.groupKey,
true,
one => one.mainKey + one.subKey
)
).toArray();
// result3: [
// {"groupKey":{"mainKey":2,"subKey":"b"},"value":"test2"},
// {"groupKey":{"mainKey":1,"subKey":"c"},"value":"test4"}
// ]
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of defaultSelector is as follows.
export const defaultSelector = (target: any): any => target;
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
target | Iterable <T > | undefined | Sequence to be removed. |
keySelector | (one : T ) => TKey | defaultSelector | Function to return the object used to check Equality.. |
removeDuplicate | boolean | true | If removeDuplicate is set to true, duplicates will be removed; default is true. |
comparableValueForKey? | (key : TKey ) => TComparableValue | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
filter
▸ filter<T
, TResult
>(predicate
): Operator
<T
, TResult
>
Returns a sequence that has been filtered by the specified condition.
const result = from([1, 2, 3, 4, 5]).pipe(filter(i => i % 2 == 0)).toArray();
//result: [2,4]
with User Defined Type Guard
const result = from([1,'a',2,'b'])
.pipe(
filter<number | string, string>((i): i is string => typeof i === 'string')
)
.toMutableArray();
//result: ['a','b']
//result type is string[]
Type parameters
Name | Description |
---|---|
T | Source element type. |
TResult | - |
Parameters
Name | Type | Description |
---|---|---|
predicate | (arg : T , index : number ) => arg is TResult | Conditional functions for filtering. |
Returns
Operator
<T
, TResult
>
Operator function.
Defined in
▸ filter<T
>(predicate
): Operator
<T
>
Returns a sequence that has been filtered by the specified condition.
const result = from([1, 2, 3, 4, 5]).pipe(filter(i => i % 2 == 0)).toArray();
//result: [2,4]
with User Defined Type Guard
const result = from([1,'a',2,'b'])
.pipe(
filter<number | string, string>((i): i is string => typeof i === 'string')
)
.toMutableArray();
//result: ['a','b']
//result type is string[]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
predicate | (arg : T , index : number ) => boolean | Conditional functions for filtering. |
Returns
Operator
<T
>
Operator function.
Defined in
finalize
▸ finalize<T
>(action
): Operator
<T
, T
>
Invokes a specified action after the source iterable sequence terminates normally or exceptionally.
const result = from([1, 2, 3, 4, 5]).pipe(
take(3)
finalize(() => console.log('finalized'))
).toArray();
//result: [1,2,3]
//console: finalized
const source = from([1, 2, 3, 4, 5]).pipe(
take(3)
finalize(() => console.log('finalized'))
);
for(const one of source) {
console.log(one)
}
//console: 1
//console: 2
//console: 3
//console: finalized
break
const source = from([1, 2, 3, 4, 5]).pipe(
take(3)
finalize(() => console.log('finalized'))
);
for(const one of source) {
console.log(one)
if(one == 2) break;
}
//console: 1
//console: 2
//console: finalized
value
const result = from([1, 2, 3, 4, 5]).pipe(
take(3)
finalize(() => console.log('finalized'))
).value(find(i == 2));
//result: 2
//console: finalized
error
const result = from([1, 2, 3, 4, 5]).pipe(
take(3)
tap(() => {throw new Error('test')})
finalize(() => console.log('finalized'))
).toArray();
//console: finalized
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
action | () => void | finalize action. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
flatten
▸ flatten<T
, TResult
>(func
): Operator
<T
, TResult
>
Returns a flattened sequence.
const result1 = from([[1,2],[3,4]]).pipe(flatten(i => i)).toArray();
//result1: [1,2,3,4]
const result2 = from([
{values:[1,2]},
{values:[3,4]}
]).pipe(
flatten(i => i.values)
).toArray();
//result2: [1,2,3,4]
Type parameters
Name | Description |
---|---|
T | Source element type. |
TResult | Result element type. |
Parameters
Name | Type | Description |
---|---|---|
func | (arg : T , index : number ) => Iterable <TResult > | A function that takes an element of a sequence and returns it in an Iterable form. |
Returns
Operator
<T
, TResult
>
Operator function.
Defined in
groupBy
▸ groupBy<T
, TComparableValue
, TKey
, TValue
>(keySelector
, elementSelector?
, comparableValueForKey?
): Operator
<T
, { key
: TKey
; values
: readonly TValue
[] }>
Returns a sequence grouped by a specified key.
const source1 = [
{groupKey: 1, value: "test1"},
{groupKey: 3, value: "test2"},
{groupKey: 1, value: "test3"},
{groupKey: 1, value: "test4"},
{groupKey: 3, value: "test5"},
{groupKey: 2, value: "test6"}
]
const result1 = from(source1).pipe(groupBy(one => one.groupKey),).toArray();
// result1: [
// {key: 1, values: [
// {groupKey: 1, value: "test1"},
// {groupKey: 1, value: "test3"},
// {groupKey: 1, value: "test4"}
// ]},
// {key: 3, values: [
// {groupKey: 3, value: "test2"},
// {groupKey: 3, value: "test5"}
// ]},
// {key: 2, values: [
// {groupKey: 2, value: "test6"}
// ]}
// ]
const source2 = [
{groupKey: {key: 1}, value: "test1"},
{groupKey: {key: 2}, value: "test2"},
{groupKey: {key: 1}, value: "test3"},
{groupKey: {key: 1}, value: "test4"},
]
const result2 = from(source2).pipe(groupBy(one => one.groupKey,one => one.value,k => k.key),).toArray();
// result2: [
// {key: {key: 1}, values: ["test1","test3","test4"]},
// {key: {key: 2}, values: ["test2"]}
// ];
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of defaultSelector is as follows.
export const defaultSelector = (target: any): any => target;
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
TValue | T | The type that will be enumerated in the Value property of the grouped result. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
keySelector | (target : T ) => TKey | undefined | Function to return the object used to check Equality.. |
elementSelector | (target : T ) => TValue | defaultSelector | Function to return the object to be enumerated in the Value property of the grouped result. |
comparableValueForKey? | (key : TKey ) => TComparableValue | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
Operator
<T
, { key
: TKey
; values
: readonly TValue
[] }>
Operator function.
Defined in
intersect
▸ intersect<T
, TComparableValue
, TKey
>(target
, keySelector?
, comparableValueForKey?
): Operator
<T
, T
>
Returns a sequence that is the product set of the current sequence and the specified sequence.
const result1 = from([1, 2, 3]).pipe(intersect([2,3,4,5])).toArray();
//result1: [2,3]
const source = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'b'}, value: "test2"},
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test3"},
{groupKey: {mainKey: 1, subKey: 'c'}, value: "test4"},
];
const target = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'e'}, value: "test2"},
];
const result2 = from(source).pipe(
intersect(
target,
i => i.groupKey,
one => one.mainKey + one.subKey
)
).toArray();
// result2: [
// {"groupKey":{"mainKey":1,"subKey":"a"},"value":"test1"}
// ]
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of defaultSelector is as follows.
export const defaultSelector = (target: any): any => target;
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
target | Iterable <T > | undefined | Sequence to be removed. |
keySelector | (one : T ) => TKey | defaultSelector | Function to return the object used to check Equality. |
comparableValueForKey? | (key : TKey ) => TComparableValue | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
map
▸ map<T
, TResult
>(func
): Operator
<T
, TResult
>
Returns the sequence in which each element has been transformed by the specified transformation function.
const result = from([1, 2, 3, 4, 5]).pipe(map(i => i * i)).toArray();
//result: [1,4,9,16,25]
Type parameters
Name | Description |
---|---|
T | Source element type. |
TResult | Transformed element type. |
Parameters
Name | Type | Description |
---|---|---|
func | (arg : T , index : number ) => TResult | Transform function. |
Returns
Operator
<T
, TResult
>
Operator function.
Defined in
orderBy
▸ orderBy<T
, TKey
>(keySelector
, sortType?
, compareFunction?
): Operator
<T
, T
>
Returns a sequence sorted by a specified key.
Internally, it uses Array.sort(), and its function is basically the same as Array.sort(), except that it is given a compareFunction by default.
const result1 = from([4,1,2,5,3]).pipe(
orderBy(i => i, 'asc')
).toArray();
//result1: [1,2,3,4,5]
const originalCompareFunction = (a: number, b:number) => {
if(a % 2 < b % 2) return - 1;
if(a % 2 > b % 2) return 1;
return 0;
}
const result2 = from([4,1,5,3,2]).pipe(
orderBy(i => i, 'asc', originalCompareFunction)
).toArray();
//result2: [4,2,1,5,3]
Also, the implementation of the default compareFunction is as follows.
const defaultSortFunction = (a: any, b: any) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
Type parameters
Name | Description |
---|---|
T | Source element type. |
TKey | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
keySelector | (arg : T ) => TKey | undefined | Function to return sort key |
sortType | SortType | 'asc' | 'asc' or 'desc' |
compareFunction | (a : TKey , b : TKey ) => number | defaultCompareFunction | See Array.sort() for more information. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
repeat
▸ repeat<T
>(count?
): Operator
<T
, T
>
Returns a sequence that repeats the source sequence a specified number of times.
const result = from([1, 2, 3]).pipe(repeat(3)).toArray();
//result: [1,2,3,1,2,3,1,2,3]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
count | number | -1 | If this argument is not specified or -1 is specified, it repeats indefinitely. If a natural number is specified, it repeats the specified number of times. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
reverse
▸ reverse<T
>(): Operator
<T
, T
>
Returns a sequence in reverse order of the current sequence.
const result = from([1, 2, 3, 4, 5]).pipe(reverse()).toArray();
//result: [5,4,3,2,1]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
scan
▸ scan<T
, TAccumulate
>(seed
, func
): Operator
<T
, TAccumulate
>
Returns the resulting sequence after applying the aggregate function to the elements of the current sequence. The difference from reduce() is that each time the aggregate function is applied, the intermediate steps are also enumerated.
const output = from([1, 2, 3, 4, 5]).pipe(
scan(100, (acc, i) => acc + i)
).toArray();
//result: [101, 103, 106, 110, 115]
Type parameters
Name | Description |
---|---|
T | Source element type. |
TAccumulate | The type returned by the aggregate function. |
Parameters
Name | Type | Description |
---|---|---|
seed | TAccumulate | This is the initial value for aggregation. |
func | (acc : TAccumulate , current : T , index : number ) => TAccumulate | Aggregate function. |
Returns
Operator
<T
, TAccumulate
>
Operator function.
Defined in
skip
▸ skip<T
>(count
): Operator
<T
, T
>
Returns the sequence with the specified number of skips.
const result = range(1,10).pipe(
skip(3)
).toArray()
//result: [4,5,6,7,8,9,10]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
count | number | Number of pieces to skip. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
skipWhile
▸ skipWhile<T
>(predicate
): Operator
<T
, T
>
Returns the sequence of elements skipped while matching the condition.
const result = range(1,10).pipe(
skipWhile(i => i < 8)
).toArray()
//result: [8,9,10]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
predicate | (arg : T ) => boolean | Condition to skip enumeration. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
take
▸ take<T
>(count
): Operator
<T
, T
>
Returns a sequence that enumerates the specified number of items.
const result = range(1,10).pipe(
take(3)
).toArray()
//result: [1,2,3]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
count | number | Number to enumerate. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
takeWhile
▸ takeWhile<T
>(predicate
): Operator
<T
, T
>
Returns a sequence to be enumerated only while the condition is matched.
const result = range(1,10).pipe(
takeWhile(i => i < 5)
).toArray()
//result: [1,2,3,4]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
predicate | (arg : T ) => boolean | Condition. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
tap
▸ tap<T
>(func
): Operator
<T
, T
>
Run side effects. The returning sequence is the same as the current one.
const result = from([1,2,3]).pipe(
tap(i => console.log(i * i))
).toArray();
//result: [1,2,3]
//console:
// 1
// 4
// 9
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
func | (arg : T , index : number ) => void | Side effects to perform |
Returns
Operator
<T
, T
>
Operator function.
Defined in
union
▸ union<T
, TComparableValue
, TKey
>(target
, keySelector?
, comparableValueForKey?
): Operator
<T
, T
>
Returns a sequence that is the union set of the current sequence and the specified sequence.
const result1 = from([1, 2, 3]).pipe(union([2,3,4,5])).toArray();
//result1: [1,2,3,4,5]
const source = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'b'}, value: "test2"},
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test3"},
{groupKey: {mainKey: 1, subKey: 'c'}, value: "test4"},
];
const target = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'e'}, value: "test2"},
];
const result2 = from(source).pipe(
union(
target,
i => i.groupKey,
one => one.mainKey + one.subKey
)
).toArray();
// result2: [
// {"groupKey":{"mainKey":1,"subKey":"a"},"value":"test1"},
// {"groupKey":{"mainKey":2,"subKey":"b"},"value":"test2"},
// {"groupKey":{"mainKey":1,"subKey":"c"},"value":"test4"},
// {"groupKey":{"mainKey":2,"subKey":"e"},"value":"test2"}
// ]
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of defaultSelector is as follows.
export const defaultSelector = (target: any): any => target;
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
target | Iterable <T > | undefined | Sequence to be removed. |
keySelector | (one : T ) => TKey | defaultSelector | Function to return the object used to check Equality. |
comparableValueForKey? | (key : TKey ) => TComparableValue | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
uniq
▸ uniq<T
, TComparableValue
, TKey
>(keySelector?
, comparableValueForKey?
): Operator
<T
, T
>
Returns a deduplicated sequence.
const result1 = from([1,1,3,2,4,4,4,1,5]).pipe(uniq()).toArray();
//result1: [1,3,2,4,5]
const source = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'b'}, value: "test2"},
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test3"},
{groupKey: {mainKey: 1, subKey: 'c'}, value: "test4"},
];
const result2 = from(source).pipe(
uniq(
i => i.groupKey,
one => one.mainKey + one.subKey
)
).toArray();
// result2: [
// {"groupKey":{"mainKey":1,"subKey":"a"},"value":"test1"},
// {"groupKey":{"mainKey":2,"subKey":"b"},"value":"test2"},
// {"groupKey":{"mainKey":1,"subKey":"c"},"value":"test4"},
// ]
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of defaultSelector is as follows.
export const defaultSelector = (target: any): any => target;
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
keySelector | (target : T ) => TKey | defaultSelector | Function to return the object used to check Equality. |
comparableValueForKey? | (key : TKey ) => TComparableValue | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
Operator
<T
, T
>
Operator function.
Defined in
zipWith
▸ zipWith<T
, T1
, T2
, T3
, T4
, T5
>(source1
, source2
, source3
, source4
, source5
): Operator
<T
, [T
, T1
, T2
, T3
, T4
, T5
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = from([1,2,3]).pipe(
zipWith(['a','b'])
).toArray();
//result1: [[1,'a'],[2,'b']]
const result2 = from([1,2,3]).pipe(
zipWith(['a','b'],[true,false,true])
).toArray();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
T2 | Second element type. |
T3 | Third element type. |
T4 | Fourth element type. |
T5 | Fifth element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | Iterable <T1 > | First iterable. |
source2 | Iterable <T2 > | Second iterable. |
source3 | Iterable <T3 > | Third iterable. |
source4 | Iterable <T4 > | Fourth iterable. |
source5 | Iterable <T5 > | Fifth iterable. |
Returns
Operator
<T
, [T
, T1
, T2
, T3
, T4
, T5
]>
[T,T1,T2,T3,T4,T5] sequence.
Defined in
▸ zipWith<T
, T1
, T2
, T3
, T4
>(source1
, source2
, source3
, source4
): Operator
<T
, [T
, T1
, T2
, T3
, T4
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = from([1,2,3]).pipe(
zipWith(['a','b'])
).toArray();
//result1: [[1,'a'],[2,'b']]
const result2 = from([1,2,3]).pipe(
zipWith(['a','b'],[true,false,true])
).toArray();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
T2 | Second element type. |
T3 | Third element type. |
T4 | Fourth element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | Iterable <T1 > | First iterable. |
source2 | Iterable <T2 > | Second iterable. |
source3 | Iterable <T3 > | Third iterable. |
source4 | Iterable <T4 > | Fourth iterable. |
Returns
Operator
<T
, [T
, T1
, T2
, T3
, T4
]>
[T,T1,T2,T3,T4] sequence.
Defined in
▸ zipWith<T
, T1
, T2
, T3
>(source1
, source2
, source3
): Operator
<T
, [T
, T1
, T2
, T3
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = from([1,2,3]).pipe(
zipWith(['a','b'])
).toArray();
//result1: [[1,'a'],[2,'b']]
const result2 = from([1,2,3]).pipe(
zipWith(['a','b'],[true,false,true])
).toArray();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
T2 | Second element type. |
T3 | Third element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | Iterable <T1 > | First iterable. |
source2 | Iterable <T2 > | Second iterable. |
source3 | Iterable <T3 > | Third iterable. |
Returns
Operator
<T
, [T
, T1
, T2
, T3
]>
[T,T1,T2,T3] sequence.
Defined in
▸ zipWith<T
, T1
, T2
>(source1
, source2
): Operator
<T
, [T
, T1
, T2
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = from([1,2,3]).pipe(
zipWith(['a','b'])
).toArray();
//result1: [[1,'a'],[2,'b']]
const result2 = from([1,2,3]).pipe(
zipWith(['a','b'],[true,false,true])
).toArray();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
T2 | Second element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | Iterable <T1 > | First iterable. |
source2 | Iterable <T2 > | Second iterable. |
Returns
Operator
<T
, [T
, T1
, T2
]>
[T,T1,T2] sequence.
Defined in
▸ zipWith<T
, T1
>(source1
): Operator
<T
, [T
, T1
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = from([1,2,3]).pipe(
zipWith(['a','b'])
).toArray();
//result1: [[1,'a'],[2,'b']]
const result2 = from([1,2,3]).pipe(
zipWith(['a','b'],[true,false,true])
).toArray();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | Iterable <T1 > | First iterable. |
Returns
Operator
<T
, [T
, T1
]>
[T,T1] sequence.
Defined in
Async Operators
catchErrorAsync
▸ catchErrorAsync<T
, TAlternative
>(action
): AsyncOperator
<T
, T
| TAlternative
>
If the original iterable sequence raises an exception, the specified action is performed, terminating the enumeration or enumerating an alternate sequence.
const result = await fromAsAsync([1,2,3]).pipe(
tapAsync(async i => {
if(i === 2) throw new Error('test');
}),
catchErrorAsync(async (e) => {
if(e instanceof Error) {
console.log(`error occurred: ${e.message}`)
}
})
).toArrayAsync();
//result: [1]
//console: error occurred: test
const result = await fromAsAsync([1,2,3]).pipe(
tapAsync(async i => {
if(i === 2) throw new Error();
}),
catchErrorAsync(async () => [4,5,6])
).toArrayAsync();
//result: [1,4,5,6]
with nested & finalize
const alternative = fromAsAsync([4,5,6]).pipe(
tapAsync(async i => {
if(i === 5) throw new Error();
}),
catchErrorAsync(async () => [7,8,9]),
finalizeAsync(async () => console.log('seq 2 finished')),
)
const result = await fromAsAsync([1,2,3]).pipe(
tapAsync(async i => {
if(i === 2) throw new Error();
}),
catchErrorAsync(async () => alternative),
finalizeAsync(async () => console.log('seq 1 finished'))
).toArrayAsync();
//result: [1,4,7,8,9]
//console: seq 2 finished
//console: seq 1 finished
different type
const output = await fromAsAsync([1,2,3]).pipe(
tapAsync(async i => {
if(i === 2) throw new Error();
}),
catchErrorAsync(async () => ['a', 'b', 'c'])
).toArrayAsync();
// type output : (number | string)[]
// output : [1, 'a', 'b', 'c']
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TAlternative | T | alternative iterable element type. |
Parameters
Name | Type | Description |
---|---|---|
action | (error : unknown ) => Promise <void > | (error : unknown ) => Promise <AllIterables <TAlternative >> | finalize action. Returns void or an alternative Iterable. |
Returns
AsyncOperator
<T
, T
| TAlternative
>
Operator function.
Defined in
chunkAsync
▸ chunkAsync<T
>(size
): AsyncOperator
<T
, readonly T
[]>
Returns a sequence divided into array of the specified size.
const result = await fromAsAsync([1, 2, 3, 4, 5, 6, 7]).pipe(
chunkAsync(2)
).toArrayAsync();
//result: [[1,2],[3,4],[5,6],[7]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
size | number | Length of elements per array. |
Returns
AsyncOperator
<T
, readonly T
[]>
Operator function.
Defined in
chunkByAccumulationAsync
▸ chunkByAccumulationAsync<T
, TAccumulate
>(seed
, func
, thresholdUntil
): AsyncOperator
<T
, readonly T
[]>
Returns a sequence divided into arrays based on an asynchronous accumulation function and a threshold condition.
const result1 = await fromAsAsync([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).pipe(
chunkByAccumulationAsync(
0,
async (acc, current) => acc + current,
async (acc) => acc <= 10
)
).toArrayAsync();
// result1: [[1, 2, 3, 4], [5], [6], [7], [8], [9], [10]]
// Example where the first value doesn't satisfy the condition
const result2 = await fromAsAsync([11, 1, 2, 3]).pipe(
chunkByAccumulationAsync(
0,
async (acc, current) => acc + current,
async (acc) => acc <= 10
)
).toArrayAsync();
// result2: []
// Example where enumeration stops when a single value doesn't satisfy the condition
const result3 = await fromAsAsync([1, 2, 3, 15, 4, 5]).pipe(
chunkByAccumulationAsync(
0,
async (acc, current) => acc + current,
async (acc) => acc <= 10
)
).toArrayAsync();
// result3: [[1, 2, 3]]
In these examples:
- A new chunk starts whenever the sum exceeds 10.
- In result2, an empty array is returned because the first value (11) immediately fails the condition.
- In result3, enumeration stops at 15 because it fails the condition on its own, and the previous chunk is returned.
Remarks
- The accumulation is reset to the seed value at the beginning of each new chunk.
- If the first value in the sequence doesn't satisfy the thresholdUntil condition, an empty sequence is returned.
- If at any point in the sequence a single value doesn't satisfy the thresholdUntil condition, the enumeration stops at that point.
Type parameters
Name | Description |
---|---|
T | Source element type. |
TAccumulate | The type returned by the aggregate function. |
Parameters
Name | Type | Description |
---|---|---|
seed | TAccumulate | The initial value for the accumulation. |
func | (accInChunk : TAccumulate , current : T ) => Promise <TAccumulate > | An asynchronous function that takes the current accumulation and the current element, and returns a Promise that resolves to a new accumulation. |
thresholdUntil | (accInChunk : TAccumulate ) => Promise <boolean > | An asynchronous function that takes the current accumulation and returns a Promise that resolves to true if the chunk should continue, or false if a new chunk should start. When this function returns false, the accumulation is reset to the seed value for the next chunk. |
Returns
AsyncOperator
<T
, readonly T
[]>
Operator function.
Defined in
async/chunkByAccumulationAsync.ts:60
concatAsync
▸ concatAsync<T
>(target
): AsyncOperator
<T
, T
>
Returns a sequence in which the current sequence and the specified sequence are concatenated.
const result = await fromAsAsync([1, 2]).pipe(
concat([3,4])
).toArrayAsync();
//result: [1,2,3,4]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
target | AllIterables <T > | Sequence to be concatenated. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
concatValueAsync
▸ concatValueAsync<T
>(target
): AsyncOperator
<T
, T
>
Returns the sequence to which the specified value is added.
const result = await fromAsAsync([1, 2]).pipe(
concatValueAsync(3)
).toArrayAsync();
//result: [1,2,3]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
target | T | Element you want to add to the sequence. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
differenceAsync
▸ differenceAsync<T
, TComparableValue
, TKey
>(target
, keySelector?
, removeDuplicate?
, comparableValueForKey?
): AsyncOperator
<T
, T
>
Returns the sequence that is the difference set between the current sequence and the specified sequence.
const result1 = await fromAsAsync([1, 2, 3, 6, 6]).pipe(
differenceAsync([2,3,4,5])
).toArrayAsync();
//result1: [1,6]
const result2 = await fromAsAsync([1, 2, 3, 6, 6]).pipe(
differenceAsync([2,3,4,5], async i => i, false)
).toArrayAsync();
//result2: [1,6,6]
const source = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'b'}, value: "test2"},
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test3"},
{groupKey: {mainKey: 1, subKey: 'c'}, value: "test4"},
];
const target = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'e'}, value: "test2"},
];
const result3 = await fromAsAsync(source).pipe(
differenceAsync(
target,
async i => i.groupKey,
true,
async one => one.mainKey + one.subKey
)
).toArrayAsync();
// result3: [
// {"groupKey":{"mainKey":2,"subKey":"b"},"value":"test2"},
// {"groupKey":{"mainKey":1,"subKey":"c"},"value":"test4"}
// ]
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of asyncDefaultSelector is as follows.
export const asyncDefaultSelector = (target: any): any => Promise.resolve(target);
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
target | AllIterables <T > | undefined | Sequence to be removed. |
keySelector | (one : T ) => Promise <TKey > | asyncDefaultSelector | Function to return the object used to check Equality.. |
removeDuplicate | boolean | true | If removeDuplicate is set to true, duplicates will be removed; default is true. |
comparableValueForKey? | (key : TKey ) => Promise <TComparableValue > | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
filterAsync
▸ filterAsync<T
, TResult
>(predicate
): AsyncOperator
<T
, TResult
>
Returns a sequence that has been filtered by the specified condition.
const result = await fromAsAsync([1, 2, 3, 4, 5]).pipe(
filterAsync(async i => i % 2 == 0)
).toArrayAsync();
//result: [2,4]
with User Defined Type Guard
const result = await fromAsAsync([1,'a',2,'b'])
.pipe(
filterAsync<number | string, string>((i): i is string => typeof i === 'string')
)
.toMutableArrayAsync();
//result: ['a','b']
//result type is string[]
Type parameters
Name | Description |
---|---|
T | Source element type. |
TResult | - |
Parameters
Name | Type | Description |
---|---|---|
predicate | (arg : T , index : number ) => arg is TResult | Conditional functions for filtering. |
Returns
AsyncOperator
<T
, TResult
>
Operator function.
Defined in
▸ filterAsync<T
>(predicate
): AsyncOperator
<T
>
Returns a sequence that has been filtered by the specified condition.
const result = await fromAsAsync([1, 2, 3, 4, 5]).pipe(
filterAsync(async i => i % 2 == 0)
).toArrayAsync();
//result: [2,4]
with User Defined Type Guard
const result = await fromAsAsync([1,'a',2,'b'])
.pipe(
filterAsync<number | string, string>((i): i is string => typeof i === 'string')
)
.toMutableArrayAsync();
//result: ['a','b']
//result type is string[]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
predicate | (arg : T , index : number ) => boolean | Promise <boolean > | Conditional functions for filtering. |
Returns
AsyncOperator
<T
>
Operator function.
Defined in
finalizeAsync
▸ finalizeAsync<T
>(action
): AsyncOperator
<T
, T
>
Invokes a specified action after the source iterable sequence terminates normally or exceptionally.
const result = await fromAsAsync([1, 2, 3, 4, 5]).pipe(
takeAsync(3),
finalizeAsync(async () => console.log('finalized'))
).toArrayAsync();
//result: [1,2,3]
//console: finalized
const source = fromAsAsync([1, 2, 3, 4, 5]).pipe(
takeAsync(3),
finalizeAsync(async () => console.log('finalized'))
);
for await (const one of source) {
console.log(one)
}
//console: 1
//console: 2
//console: 3
//console: finalized
break
const source = fromAsAsync([1, 2, 3, 4, 5]).pipe(
takeAsync(3),
finalizeAsync(async () => console.log('finalized'))
);
for await (const one of source) {
console.log(one)
if(one == 2) break;
}
//console: 1
//console: 2
//console: finalized
value
const result = await fromAsAsync([1, 2, 3, 4, 5]).pipe(
takeAsync(3),
finalizeAsync(async () => console.log('finalized'))
).valueAsync(findAsync(async i => i == 2));
//result: 2
//console: finalized
error
const result = await fromAsAsync([1, 2, 3, 4, 5]).pipe(
takeAsync(3),
tapAsync(async () => {throw new Error('test')}),
finalizeAsync(async () => console.log('finalized'))
).toArrayAsync();
//console: finalized
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
action | () => Promise <void > | finalize action. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
flattenAsync
▸ flattenAsync<T
, TResult
>(func
): AsyncOperator
<T
, TResult
>
Returns a flattened sequence.
const result1 = await fromAsAsync([[1,2],[3,4]]).pipe(
flattenAsync(async i => i)
).toArrayAsync();
//result1: [1,2,3,4]
const result2 = await fromAsAsync([
{values:[1,2]},
{values:[3,4]}
]).pipe(
flattenAsync(async i => i.values)
).toArrayAsync();
//result2: [1,2,3,4]
Type parameters
Name | Description |
---|---|
T | Source element type. |
TResult | Result element type. |
Parameters
Name | Type | Description |
---|---|---|
func | (arg : T , index : number ) => Promise <AllIterables <TResult >> | A function that takes an element of a sequence and returns it in an Iterable form. |
Returns
AsyncOperator
<T
, TResult
>
Operator function.
Defined in
groupByAsync
▸ groupByAsync<T
, TComparableValue
, TKey
, TValue
>(keySelector
, elementSelector?
, comparableValueForKey?
): AsyncOperator
<T
, { key
: TKey
; values
: readonly TValue
[] }>
Returns a sequence grouped by a specified key.
const source1 = [
{groupKey: 1, value: "test1"},
{groupKey: 3, value: "test2"},
{groupKey: 1, value: "test3"},
{groupKey: 1, value: "test4"},
{groupKey: 3, value: "test5"},
{groupKey: 2, value: "test6"}
]
const result1 = await fromAsAsync(source1).pipe(
groupByAsync(async one => one.groupKey)
).toArrayAsync();
// result1: [
// {key: 1, values: [
// {groupKey: 1, value: "test1"},
// {groupKey: 1, value: "test3"},
// {groupKey: 1, value: "test4"}
// ]},
// {key: 3, values: [
// {groupKey: 3, value: "test2"},
// {groupKey: 3, value: "test5"}
// ]},
// {key: 2, values: [
// {groupKey: 2, value: "test6"}
// ]}
// ]
const source2 = [
{groupKey: {key: 1}, value: "test1"},
{groupKey: {key: 2}, value: "test2"},
{groupKey: {key: 1}, value: "test3"},
{groupKey: {key: 1}, value: "test4"},
]
const result2 = await fromAsAsync(source2).pipe(
groupByAsync(
async one => one.groupKey,
async one => one.value,
async k => k.key
)).toArrayAsync();
// result2: [
// {key: {key: 1}, values: ["test1","test3","test4"]},
// {key: {key: 2}, values: ["test2"]}
// ];
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of asyncDefaultSelector is as follows.
export const asyncDefaultSelector = (target: any): any => Promise.resolve(target);
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
TValue | T | The type that will be enumerated in the Value property of the grouped result. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
keySelector | (target : T ) => Promise <TKey > | undefined | Function to return the object used to check Equality.. |
elementSelector | (target : T ) => Promise <TValue > | asyncDefaultSelector | Function to return the object to be enumerated in the Value property of the grouped result. |
comparableValueForKey? | (key : TKey ) => Promise <TComparableValue > | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
AsyncOperator
<T
, { key
: TKey
; values
: readonly TValue
[] }>
Operator function.
Defined in
intersectAsync
▸ intersectAsync<T
, TComparableValue
, TKey
>(target
, keySelector?
, comparableValueForKey?
): AsyncOperator
<T
, T
>
Returns a sequence that is the product set of the current sequence and the specified sequence.
const result1 = await fromAsAsync([1, 2, 3]).pipe(
intersectAsync([2,3,4,5])
).toArrayAsync();
//result1: [2,3]
const source = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'b'}, value: "test2"},
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test3"},
{groupKey: {mainKey: 1, subKey: 'c'}, value: "test4"},
];
const target = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'e'}, value: "test2"},
];
const result2 = await fromAsAsync(source).pipe(
intersectAsync(
target,
async i => i.groupKey,
async one => one.mainKey + one.subKey
)
).toArrayAsync();
// result2: [
// {"groupKey":{"mainKey":1,"subKey":"a"},"value":"test1"}
// ]
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of asyncDefaultSelector is as follows.
export const asyncDefaultSelector = (target: any): any => Promise.resolve(target);
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
target | AllIterables <T > | undefined | Sequence to be removed. |
keySelector | (one : T ) => Promise <TKey > | asyncDefaultSelector | Function to return the object used to check Equality. |
comparableValueForKey? | (key : TKey ) => Promise <TComparableValue > | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
mapAsync
▸ mapAsync<T
, TResult
>(func
): AsyncOperator
<T
, TResult
>
Returns the sequence in which each element has been transformed by the specified transformation function.
const result = await fromAsAsync([1, 2, 3, 4, 5]).pipe(
mapAsync(async i => i * i)
).toArrayAsync();
//result: [1,4,9,16,25]
Type parameters
Name | Description |
---|---|
T | Source element type. |
TResult | Transformed element type. |
Parameters
Name | Type | Description |
---|---|---|
func | (arg : T , index : number ) => Promise <TResult > | Transform function. |
Returns
AsyncOperator
<T
, TResult
>
Operator function.
Defined in
orderByAsync
▸ orderByAsync<T
, TKey
>(keySelector
, sortType?
, compareFunction?
): AsyncOperator
<T
, T
>
Returns a sequence sorted by a specified key.
Internally, it uses Array.sort(), and its function is basically the same as Array.sort(), except that it is given a compareFunction by default.
const result1 = await fromAsAsync([4,1,2,5,3]).pipe(
orderByAsync(i => i, 'asc')
).toArrayAsync();
//result1: [1,2,3,4,5]
const originalCompareFunction = (a: number, b:number) => {
if(a % 2 < b % 2) return - 1;
if(a % 2 > b % 2) return 1;
return 0;
}
const result2 = await fromAsAsync([4,1,5,3,2]).pipe(
orderByAsync(i => i, 'asc', originalCompareFunction)
).toArrayAsync();
//result2: [4,2,1,5,3]
Also, the implementation of the default compareFunction is as follows.
const defaultSortFunction = (a: any, b: any) => {
if (a < b) return -1;
if (a > b) return 1;
return 0;
};
Type parameters
Name | Description |
---|---|
T | Source element type. |
TKey | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
keySelector | (arg : T ) => TKey | undefined | Function to return sort key |
sortType | SortType | 'asc' | 'asc' or 'desc' |
compareFunction | (a : TKey , b : TKey ) => number | defaultCompareFunction | See Array.sort() for more information. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
repeatAsync
▸ repeatAsync<T
>(count?
): AsyncOperator
<T
, T
>
Returns a sequence that repeats the source sequence a specified number of times.
const result = await fromAsAsync([1, 2, 3]).pipe(repeatAsync(3)).toArrayAsync();
//result: [1,2,3,1,2,3,1,2,3]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
count | number | -1 | If this argument is not specified or -1 is specified, it repeats indefinitely. If a natural number is specified, it repeats the specified number of times. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
reverseAsync
▸ reverseAsync<T
>(): AsyncOperator
<T
, T
>
Returns a sequence in reverse order of the current sequence.
const result = await fromAsAsync([1, 2, 3, 4, 5]).pipe(
reverseAsync()
).toArrayAsync();
//result: [5,4,3,2,1]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
scanAsync
▸ scanAsync<T
, TAccumulate
>(seed
, func
): AsyncOperator
<T
, TAccumulate
>
Returns the resulting sequence after applying the aggregate function to the elements of the current sequence. The difference from reduce() is that each time the aggregate function is applied, the intermediate steps are also enumerated.
const output = await fromAsAsync([1, 2, 3, 4, 5]).pipe(
scanAsync(100, async (acc, i) => acc + i)
).toArrayAsync();
//result: [101, 103, 106, 110, 115]
Type parameters
Name | Description |
---|---|
T | Source element type. |
TAccumulate | The type returned by the aggregate function. |
Parameters
Name | Type | Description |
---|---|---|
seed | TAccumulate | This is the initial value for aggregation. |
func | (acc : TAccumulate , current : T , index : number ) => Promise <TAccumulate > | Aggregate function. |
Returns
AsyncOperator
<T
, TAccumulate
>
Operator function.
Defined in
skipAsync
▸ skipAsync<T
>(count
): AsyncOperator
<T
, T
>
Returns the sequence with the specified number of skips.
const result = await rangeAsAsync(1,10).pipe(
skipAsync(3)
).toArrayAsync()
//result: [4,5,6,7,8,9,10]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
count | number | Number of pieces to skip. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
skipWhileAsync
▸ skipWhileAsync<T
>(predicate
): AsyncOperator
<T
, T
>
Returns the sequence of elements skipped while matching the condition.
const result = await rangeAsAsync(1,10).pipe(
skipWhileAsync(async i => i < 8)
).toArrayAsync()
//result: [8,9,10]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
predicate | (arg : T ) => boolean | Promise <boolean > | Condition to skip enumeration. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
takeAsync
▸ takeAsync<T
>(count
): AsyncOperator
<T
, T
>
Returns a sequence that enumerates the specified number of items.
const result = await rangeAsAsync(1,10).pipe(
takeAsync(3)
).toArrayAsync()
//result: [1,2,3]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
count | number | Number to enumerate. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
takeWhileAsync
▸ takeWhileAsync<T
>(predicate
): AsyncOperator
<T
, T
>
Returns a sequence to be enumerated only while the condition is matched.
const result = await rangeAsAsync(1,10).pipe(
takeWhileAsync(async i => i < 5)
).toArrayAsync()
//result: [1,2,3,4]
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
predicate | (arg : T ) => boolean | Promise <boolean > | Condition. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
tapAsync
▸ tapAsync<T
>(func
): AsyncOperator
<T
, T
>
Run side effects. The returning sequence is the same as the current one.
const result = fromAsAsync([1,2,3]).pipe(
tapAsync(async i => console.log(i * i))
).toArrayAsync();
//result: [1,2,3]
//console:
// 1
// 4
// 9
Type parameters
Name | Description |
---|---|
T | Source element type. |
Parameters
Name | Type | Description |
---|---|---|
func | (arg : T , index : number ) => Promise <void > | Side effects to perform |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
unionAsync
▸ unionAsync<T
, TComparableValue
, TKey
>(target
, keySelector?
, comparableValueForKey?
): AsyncOperator
<T
, T
>
Returns a sequence that is the union set of the current sequence and the specified sequence.
const result1 = await fromAsAsync([1, 2, 3]).pipe(
unionAsync([2,3,4,5])
).toArrayAsync();
//result1: [1,2,3,4,5]
const source = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'b'}, value: "test2"},
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test3"},
{groupKey: {mainKey: 1, subKey: 'c'}, value: "test4"},
];
const target = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'e'}, value: "test2"},
];
const result2 = await fromAsAsync(source).pipe(
unionAsync(
target,
async i => i.groupKey,
async one => one.mainKey + one.subKey
)
).toArrayAsync();
// result2: [
// {"groupKey":{"mainKey":1,"subKey":"a"},"value":"test1"},
// {"groupKey":{"mainKey":2,"subKey":"b"},"value":"test2"},
// {"groupKey":{"mainKey":1,"subKey":"c"},"value":"test4"},
// {"groupKey":{"mainKey":2,"subKey":"e"},"value":"test2"}
// ]
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of asyncDefaultSelector is as follows.
export const asyncDefaultSelector = (target: any): any => Promise.resolve(target);
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
target | AllIterables <T > | undefined | Sequence to be removed. |
keySelector | (one : T ) => Promise <TKey > | asyncDefaultSelector | Function to return the object used to check Equality. |
comparableValueForKey? | (key : TKey ) => Promise <TComparableValue > | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
uniqAsync
▸ uniqAsync<T
, TComparableValue
, TKey
>(keySelector?
, comparableValueForKey?
): AsyncOperator
<T
, T
>
Returns a deduplicated sequence.
const result1 = await fromAsAsync([1,1,3,2,4,4,4,1,5]).pipe(
uniqAsync()
).toArrayAsync();
//result1: [1,3,2,4,5]
const source = [
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test1"},
{groupKey: {mainKey: 2, subKey: 'b'}, value: "test2"},
{groupKey: {mainKey: 1, subKey: 'a'}, value: "test3"},
{groupKey: {mainKey: 1, subKey: 'c'}, value: "test4"},
];
const result2 = await fromAsAsync(source).pipe(
uniqAsync(
async i => i.groupKey,
async one => one.mainKey + one.subKey
)
).toArrayAsync();
// result2: [
// {"groupKey":{"mainKey":1,"subKey":"a"},"value":"test1"},
// {"groupKey":{"mainKey":2,"subKey":"b"},"value":"test2"},
// {"groupKey":{"mainKey":1,"subKey":"c"},"value":"test4"},
// ]
For more information on keySelector and comparableValueForKey, please refer to Equality Strategy.
The implementation of asyncDefaultSelector is as follows.
export const asyncDefaultSelector = (target: any): any => Promise.resolve(target);
Type parameters
Name | Type | Description |
---|---|---|
T | T | Source element type. |
TComparableValue | TComparableValue | The type of the return value returned by comparableValueForKey. |
TKey | T | key type. |
Parameters
Name | Type | Default value | Description |
---|---|---|---|
keySelector | (target : T ) => Promise <TKey > | asyncDefaultSelector | Function to return the object used to check Equality. |
comparableValueForKey? | (key : TKey ) => Promise <TComparableValue > | undefined | This function returns an object that is unique to the key selected by keySelector. It is recommended to return a string or number. |
Returns
AsyncOperator
<T
, T
>
Operator function.
Defined in
zipWithAsync
▸ zipWithAsync<T
, T1
, T2
, T3
, T4
, T5
>(source1
, source2
, source3
, source4
, source5
): AsyncOperator
<T
, [T
, T1
, T2
, T3
, T4
, T5
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'])
).toArrayAsync();
//result1: [[1,'a'],[2,'b']]
const result2 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'],[true,false,true])
).toArrayAsync();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
T2 | Second element type. |
T3 | Third element type. |
T4 | Fourth element type. |
T5 | Fifth element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | AllIterables <T1 > | First iterable. |
source2 | AllIterables <T2 > | Second iterable. |
source3 | AllIterables <T3 > | Third iterable. |
source4 | AllIterables <T4 > | Fourth iterable. |
source5 | AllIterables <T5 > | Fifth iterable. |
Returns
AsyncOperator
<T
, [T
, T1
, T2
, T3
, T4
, T5
]>
[T,T1,T2,T3,T4,T5] sequence.
Defined in
▸ zipWithAsync<T
, T1
, T2
, T3
, T4
>(source1
, source2
, source3
, source4
): AsyncOperator
<T
, [T
, T1
, T2
, T3
, T4
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'])
).toArrayAsync();
//result1: [[1,'a'],[2,'b']]
const result2 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'],[true,false,true])
).toArrayAsync();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
T2 | Second element type. |
T3 | Third element type. |
T4 | Fourth element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | AllIterables <T1 > | First iterable. |
source2 | AllIterables <T2 > | Second iterable. |
source3 | AllIterables <T3 > | Third iterable. |
source4 | AllIterables <T4 > | Fourth iterable. |
Returns
AsyncOperator
<T
, [T
, T1
, T2
, T3
, T4
]>
[T,T1,T2,T3,T4] sequence.
Defined in
▸ zipWithAsync<T
, T1
, T2
, T3
>(source1
, source2
, source3
): AsyncOperator
<T
, [T
, T1
, T2
, T3
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'])
).toArrayAsync();
//result1: [[1,'a'],[2,'b']]
const result2 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'],[true,false,true])
).toArrayAsync();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
T2 | Second element type. |
T3 | Third element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | AllIterables <T1 > | First iterable. |
source2 | AllIterables <T2 > | Second iterable. |
source3 | AllIterables <T3 > | Third iterable. |
Returns
AsyncOperator
<T
, [T
, T1
, T2
, T3
]>
[T,T1,T2,T3] sequence.
Defined in
▸ zipWithAsync<T
, T1
, T2
>(source1
, source2
): AsyncOperator
<T
, [T
, T1
, T2
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'])
).toArrayAsync();
//result1: [[1,'a'],[2,'b']]
const result2 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'],[true,false,true])
).toArrayAsync();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
T2 | Second element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | AllIterables <T1 > | First iterable. |
source2 | AllIterables <T2 > | Second iterable. |
Returns
AsyncOperator
<T
, [T
, T1
, T2
]>
[T,T1,T2] sequence.
Defined in
▸ zipWithAsync<T
, T1
>(source1
): AsyncOperator
<T
, [T
, T1
]>
Returns a sequence of arrays consisting of the elements of the source array and the elements of the multiple sequences given as arguments, concatenated one by one.
const result1 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'])
).toArrayAsync();
//result1: [[1,'a'],[2,'b']]
const result2 = await fromAsAsync([1,2,3]).pipe(
zipWithAsync(['a','b'],[true,false,true])
).toArrayAsync();
//result2: [[1,'a',true],[2,'b',false]]
Type parameters
Name | Description |
---|---|
T | Source element type. |
T1 | First element type. |
Parameters
Name | Type | Description |
---|---|---|
source1 | AllIterables <T1 > | First iterable. |
Returns
AsyncOperator
<T
, [T
, T1
]>
[T,T1] sequence.