itiriri-async
Next generation library to manipulate asynchronous iterators.
;; type ToDo = id: number userId: number title: string completed: boolean; { let id = 1; while true await WebRequestjson<ToDo>`https://jsonplaceholder.typicode.com/todos/`; } : Promise<void> { const todos = await ; console;} ;// [ 'delectus aut autem', 'quis ut nam facilis et officia qui' ]
Check examples folder for more
Installation
Using npm:
$ npm install 'itiriri-async' --save
Importing:
;
Running tests
$ npm install$ npm test
Complete list of methods
- average
- awaitAll
- concat
- distinct
- entries
- every
- exclude
- filter
- find
- findIndex
- findLast
- findLastIndex
- first
- flat
- forEach
- groupJoin
- includes
- indexOf
- intersect
- join
- keys
- last
- lastIndexOf
- leftJoin
- length
- map
- max
- min
- nth
- prepend
- reduce
- skip
- slice
- some
- sum
- take
- union
- values
average
Returns the average value.
Syntax
: Promise<number>;: Promise<number>;
Parameters
selector
- (optional) a value transformer function to apply to each element
For a sequence with no elements returns undefined
.
Example
; { 41 42 43;} { value: 1 value: 2;} // returns Promise<42> // returns Promise<1.5>
awaitAll
Awaits for all elements an returns IterableQuery
.
The ruterned iterable is a sync itiriri
iterable.
Syntax
: Promise<IterableQuery<T>>
Example
; { 41 40 43;} // ...const numbers = await ;// returns IterableQuery([41, 40, 43]) numbers;// returns: [40, 41, 43]
concat
Concatenates the sequence with another one.
Syntax
: AsyncIterableQuery<T>;: AsyncIterableQuery<T>;: AsyncIterableQuery<T>;: AsyncIterableQuery<T>;
Parameters
other
- (required) sequence to concatenate
Example
; { 1 2 3;} { 4 5;} { const q = await ; q; // returns [1, 2, 3, 4, 5]}; { const q = await ; q; // returns [1, 2, 3, 2, 1]}; { const q = await ; q; // returns [1, 2, 3, -1]};
concat
is a deferred method and is executed only when the result sequence is iterated.
distinct
Returns a sequence of unique elements.
Syntax
: AsyncIterableQuery<T>;distinct<S> S: AsyncIterableQuery<T>;
Parameters
selector
- (optional) a value transformer function to be used for comparisons
Example
; { 1 2 3 3 3 4 2;} { const q = await ; q; // returns [1, 2, 3, 4]};
distinct
is a deferred method and is executed only when the result sequence is iterated.
entries
Returns a sequence of key/value pair for each element and its index.
Syntax
: AsyncIterableQuery<number T>;
Example
; { 'Bob' 'Alice';} { const q = await ; q; // returns [[0, 'Bob'], [1, 'Alice']]};
entries
is a deferred method and is executed only when the result sequence is iterated.
every
Tests whether all the elements pass the predicate.
Syntax
: Promise<boolean>;
Parameters
predicate
- (required) function to test for each element
Example
; { 1 4 3 0;} { await ; // true}; { await ; // false};
exclude
Returns a sequence of elements not contained in a given sequence.
Syntax
exclude<S>others: Iterable<T>: AsyncIterableQuery<T>;exclude<S>others: Iterable<T> S: AsyncIterableQuery<T>;
Parameters
others
- (required) a sequence of elements to be excludedselector
- (optional) a value transformer function to be used for comparisons
Example
; { 2 0 1 8 2;} { id: 1 id: 2 ;} { const q = await ; q; // returns [2, 8, 2]}; { const q = await ; q; // returns [{id: 1}]};
exclude
is a deferred method and is executed only when the result sequence is iterated.
filter
Returns a sequence of elements that pass the predicate.
Syntax
: AsyncIterableQuery<T>;
Parameters
predicate
- (required) function to test for each element
Example
; { 1 2 3 4 5;} { const q = await ; q; // returns [1, 2]}; { const q = await ; q; // returns []};
filter
is a deferred method and is executed only when the result sequence is iterated.
find
Finds the first element that satisfies the specified predicate.
Syntax
: Promise<T>;
Parameters
predicate
- (required) function to test for each element
If no element satisfies the predicate, returns undefined
.
Example
; { 1 2 3 4 5;} { await ; // returns 3}; { await ; // returns undefined};
findIndex
Finds the first index at which a given element satisfies the specified predicate.
Syntax
: Promise<number>;
Parameters
predicate
- (required) function to test for each element
If no element satisfies the predicate, returns -1
.
Example
; { 1 2 3 4 5;} { await ; // returns 2}; { await ; // returns -1};
findLast
Finds the last element that satisfies the specified predicate.
Syntax
: Promise<T>;
Parameters
predicate
- (required) function to test for each element
If no element satisfies the predicate, returns undefined
.
Example
; { 1 2 3 4 5;} { await ; // returns 5}; { await ; // returns undefined};
findLastIndex
Finds the last index at which a given element satisfies the specified predicate.
Syntax
: Promise<number>;
Parameters
predicate
- (required) function to test for each element
If not present, returns -1
.
Example
; { 1 2 3 4 5;} { await ; // returns 4}; { await ; // returns -1};
first
Returns the first element in a sequence.
Syntax
: Promise<T>;
For an empty sequence returns undefined
.
Example
; { 1 2 3 4 5;} { ;} { await ; // returns 1}; { await ; // returns undefined};
flat
Returns a sequence with all sub-sequences concatenated.
Syntax
flat<T>selector?: AsyncIterable<S>: AsyncIterableQuery<T>;
Parameters
selector
- (optional) a value transformer function to be used for comparisons
Example
; { 1 2 3 4 5;} { const q = await ; q; // returns [1, 2, 3, 4, 5]};
flat
is a deferred method and is executed only when the result sequence is iterated.
forEach
Runs through every element and applies a given function.
Syntax
: Promise<void>;
Parameters
action
- (required) function to apply on each element
Example
; { 1 2 3;} { await ;};// 1// 2// 3
groupJoin
Returns a sequence of correlated elements where each element from the current sequence is matched with zero or more elements from the other sequence.
Syntax
groupJoin<TKey TRight TResult> other: Iterable<TRight> TKey TKey TResult : AsyncIterableQuery<TResult>;
Parameters
other
- (required) sequence to joinleftKeySelector
- (required) function that provides the key of each element from source sequencerightKeySelector
- (required) function that provides the key of each element from joined sequencejoinSelector
- (required) a transformation function to apply on each joined element with group
The joinSelector
function is called on each element from the source sequence and the array of matched
elements from the joined sequence.
When an element from the source sequence doesn't match with any of the elements from the joined sequence,
the joinSelector
function will be called with an empty array.
Example
; { 1 2 3;} { const q = await ; q;};//[ { x: 1, y: [ 1, 1, 1 ] },// { x: 2, y: [ 2, 2 ] },// { x: 3, y: [ 3 ] } ]
groupJoin
is a deferred method and is executed only when the result sequence is iterated.
includes
Determines whether the sequence includes a certain element.
Syntax
: Promise<boolean>;: Promise<boolean>;
Parameters
element
- (required) the element to search forfromIndex
- (optional) starting index, defaults to0
includes
uses triple equals ===
to compare elements.
Example
; { 1 2 3;} { await ; // returns: true await ; // returns: false};
indexOf
Returns the first (zero-based) index at which a given element can be found.
Syntax
: Promise<number>;: Promise<number>;
Parameters
element
- (required) the element to search forfromIndex
- (optional) starting index, defaults to0
When an element is not found, returns -1.
indexOf
uses triple equals ===
to compare elements.
Example
; { 1 2 3;} { await ; // returns: 1 await ; // returns: -1};
intersect
Returns a set intersection with a given sequence.
Syntax
: AsyncIterableQuery<T>;intersect<S>other: Iterable<T> S: AsyncIterableQuery<T>;
Parameters
other
- (required) the sequence to intersect withselector
- (optional) a value transformer function to be used for comparisons
Example
; { 1 2 3;} { const q = await ; q; // returns: [1, 2]};
intersect
is a deferred method and is executed only when the result sequence is iterated.
join
Returns a sequence of correlated elements transformation that match a given key.
Syntax
join<TKey TRight TResult> other: Iterable<TRight> TKey TKey TResult : AsyncIterableQuery<TResult>;
Parameters
other
- (required) sequence to joinleftKeySelector
- (required) function that provides the key of each element from source sequencerightKeySelector
- (required) function that provides the key of each element from joined sequencejoinSelector
- (required) a transformation function to apply on each matched tuple
The join
method works as an sql inner join.
Example
; { 1 2 3;} { const q = await ; q; // returns: [ { x: 1, y: 1 }, { x: 1, y: 1 }, { x: 2, y: 2 } ]};
join
is a deferred method and is executed only when the result sequence is iterated.
keys
Returns a sequence of keys for each index in the source sequence.
Syntax
: AsyncIterableQuery<number>;
Example
; { 'a' 'b' 'c';} { const q = await ; q; // returns: [0, 1, 2]};
keys
is a deferred method and is executed only when the result sequence is iterated.
last
Returns the last element in a sequence.
Syntax
: Promise<T>;
For an empty sequence returns undefined
.
Example
; { 1 2 3 -2;} { await ; // returns: -2};
lastIndexOf
Returns the last index at which a given element can be found.
Syntax
: Promise<number>;: Promise<number>;
Parameters
element
- (required) the element to search forfromIndex
- (optional) starting index, defaults to0
When an element is not found, returns -1.
lastIndexOf
uses triple equals ===
to compare elements.
Example
; { 1 2 3 2 1;} { await ; // returns: 3};
leftJoin
Returns a sequence of correlated elements transformation that match a given key.
Syntax
leftJoin<TKey TRight TResult> other: Iterable<TRight> TKey TKey TResult : AsyncIterableQuery<TResult>;
Parameters
other
- (required) sequence to joinleftKeySelector
- (required) function that provides the key of each element from source sequencerightKeySelector
- (required) function that provides the key of each element from joined sequencejoinSelector
- (required) a transformation function to apply on each matched tuple
The leftJoin
method works as an sql left join.
When an element from the left sequence doesn't match with any of the elements from the right sequence,
the joinSelector
function is called with an undefined
right value.
Example
; { 1 2 3;} { const q = await ; q; // returns ['1-#', '2-2', '2-2', '3-3']};
leftJoin
is a deferred method and is executed only when the result sequence is iterated.
length
Returns the number of elements in a sequence.
Syntax
: Promise<number>;: Promise<number>;
Parameters
predicate
- (optional) a function to count only the elements that match the predicate
Example
; { 1 2 3;} { await length; // returns 3};
map
Returns a sequence of transformed values.
Syntax
map<S> S: AsyncIterableQuery<S>;
Parameters
selector
- (required) a value transformer function to apply to each element
Example
; { 1 2 3;} { return ;} { const numbers = await ; console; // [10, 20, 30]};
map
is a deferred method and is executed only when the result sequence is iterated.
max
Returns the maximum element in a sequence.
Syntax
: Promise<T>;: Promise<T>;
Parameters
compareFn
- (optional) a comparer function that compares two elements from a sequence and returns:-1
whena
is less thanb
1
whena
is greaterb
0
whena
equals tob
If sequence is empty, returns undefined
.
Example
{ 1 42 3;} { ;} { await ; // returns 42 await ; // returns undefined};
min
Returns the minimum element in a sequence.
Syntax
: Promise<T>;: Promise<T>;
Parameters
compareFn
- (optional) a comparer function that compares two elements from a sequence and returns:-1
whena
is less thanb
1
whena
is greaterb
0
whena
equals tob
If sequence is empty, returns undefined
.
Example
; { 1 -2 3;} { ;} { await ; // returns -1 await ; // returns undefined};
nth
Returns the element at a specified index.
Syntax
: Promise<T>;
Parameters
index
- (required) zero based index at which to get the element
If index is out of the range, returns undefined
.
Example
; { 1 -2 3;} { await ; // returns: 3 await ; // returns: undefined};
prepend
Returns a sequence with given elements at the beginning.
Syntax
: AsyncIterableQuery<T>;: AsyncIterableQuery<T>;: AsyncIterableQuery<T>;: AsyncIterableQuery<T>;
Parameters
other
- (required) the sequence to be added at the beginning
Example
; { 1 -2 3;} { const q = await ; q; // returns: [4, 1, -2, 3]}; { const q = await ; q; // returns: [0, 4, 1, -2, 3]};
prepend
is a deferred method and is executed only when the result sequence is iterated.
reduce
Applies a function against an accumulator and each element (from left to right) to reduce it to a single value.
Syntax
: Promise<T>; reduce<S> S initialValue: S : Promise<S>;
Parameters
callback
- (required) function to execute on each element in the sequence, taking three argumentsaccumulator
the accumulator accumulates the callback's return values;current
the current element being processed;currentIndex
the index of the current element being processed;
initialValue
- (optional) value to use as the first argument to the first call of thecallback
Calling reduce
on an empty sequence without an initial value throws an error.
Example
; { 1 2 3;} { // 5 + 10 + 20 + 30 await ; // returns 65};
skip
Skips the specified number of elements from the beginning of sequence and returns the remaining ones.
Syntax
: AsyncIterableQuery<T>;
Parameters
count
- (required) number of elements to skip
When count is greater than actual number of elements, results in an empty sequence.
Example
; { 1 2 3;} { const q = await ; q; // returns: [2, 3]}; { const q = await ; q; // returns: []};
skip
is a deferred method and is executed only when the result sequence is iterated.
slice
Returns a sequence that represents the range of elements from start to end.
Syntax
: AsyncIterableQuery<T>;
Parameters
start
- (required) zero-based index at which to begin extractionend
- (required) zero-based index before which to end extraction.
The end
index is not included in the result.
Example
; { 1 2 3 3 4;} { const q = await ; q; // returns: [3, 3]};
slice
is a deferred method and is executed only when the result sequence is iterated.
some
Tests whether at least one element passes the predicate.
Syntax
: Promise<boolean>;
Parameters
predicate
- (required) function to test for each element
Example
; { 1 2 3 -3 4 0;} { await ; // returns: true await ; // returns: false};
sum
Returns the sum of all elements.
Syntax
: number;: Promise<number>;
Parameters
selector
- (optional) a value transformer function to apply to each element
Optionally, a function can be provided to apply a transformation and map each element to a value.
Example
; { 1 2 3;} { val: 3 val: 5 ;} { await ; // returns: 6 await ; // returns: 8};
take
Returns a specified number of elements from the beginning of sequence.
Syntax
: AsyncIterableQuery<T>;
Parameters
count
- (required) number of elements to take
Example
; { 1 2 3;} { const q = await ; q; // returns: [1, 2]}; { const q = await ; q; // returns: []};
take
is a deferred method and is executed only when the result sequence is iterated.
union
Returns a set union with a given sequence.
Syntax
: AsyncIterableQuery<T>;union<S>other: AsyncIterable<T> S: AsyncIterableQuery<T>;
Parameters
other
- (required) the sequence to join withselector
- (optional) a value transformer function to be used for comparisons
Example
; { 1 2 3;} { 3 4 5;} { const q = await ; q; // returns [1, 2, 3, 4, 5]};
union
is a deferred method and is executed only when the result sequence is iterated.
values
Returns a sequence of values for each index in the source sequence.
Syntax
: AsyncIterableQuery<T>;
Example
; { 1 0 2 3;} { const q = await ; q; // [1, 0, 2, 3]};
values
is a deferred method and is executed only when the result sequence is iterated.