Finishers
Finisher is a function that receives an Iterable as an argument, and returns a single value, terminating a query.
Aggregate
Creates a Finisher that calculates a single aggregated value by applying the accumulator function to each item of the source Iterable.
function aggregate<T, R>(
seed: R,
accumulator: (result: R, item: T) => R
): Finisher<T, R>function aggregate<T, R>(
seed: R,
accumulator: (result: R, item: T) => R
): Finisher<T, R>Arguments
seed:R- The initial value of the aggregationaccumulator:(result: R, item: T) => R- A function that computes the aggregated value by the previous aggregated value and the current item
Example
const result = query(
from([1, 2, 3]),
aggregate(0, (result, item) => result + item)
)
console.log(result)const result = query(
from([1, 2, 3]),
aggregate(0, (result, item) => result + item)
)
console.log(result)Output
66All
Creates a Finisher that checks whether all items of the source Iterable satisfy the given predicate. Returns true if the source is empty.
function all<T>(predicate: (item: T) => boolean): Finisher<T, boolean>function all<T>(predicate: (item: T) => boolean): Finisher<T, boolean>Arguments
predicate:(item: T) => boolean- A function that tests an item
Example
const result = query(
from([1, 2, 3]),
all(item => item < 2)
)
console.log(result)const result = query(
from([1, 2, 3]),
all(item => item < 2)
)
console.log(result)Output
falsefalseAny
Creates a Finisher that checks if any item of the source Iterable satisfies the given predicate. If there is no predicate given, the operator will simply check if the source is empty.
function any<T>(predicate?: (item: T) => boolean): Finisher<T, boolean>function any<T>(predicate?: (item: T) => boolean): Finisher<T, boolean>Arguments
predicate:(item: T) => boolean(optional) - A function that tests an item
Example
const result = query(
from([1, 2, 3]),
any(item => item < 2)
)
console.log(result)const result = query(
from([1, 2, 3]),
any(item => item < 2)
)
console.log(result)Output
truetrueAverage
Creates a Finisher that calculates the average of items of an Iterable<number> source.
function average(): Finisher<number, number>function average(): Finisher<number, number>Example
const result = query(
from([1, 2, 3]),
average()
)
console.log(result)const result = query(
from([1, 2, 3]),
average()
)
console.log(result)Output
22AverageOf
Creates a Finisher that calculates the average of the values mapped from the source Iterable by the given selector.
function averageOf<T>(
selector: (item: T) => number
): Finisher<T, number>function averageOf<T>(
selector: (item: T) => number
): Finisher<T, number>Arguments
selector:(item: T) => number- A function that transforms an item to a number that will be used for calculating the average
Example
const people = [
{
name: "John",
age: 25
},
{
name: "Jane",
age: 20
},
]
const result = query(
from(people),
averageOf(person => person.age)
)
console.log(result)const people = [
{
name: "John",
age: 25
},
{
name: "Jane",
age: 20
},
]
const result = query(
from(people),
averageOf(person => person.age)
)
console.log(result)Output
22.522.5Contains
Creates a Finisher that checks if the source Iterable contains an item that is equal to the provided value.
function contains<T>(
item: T,
equalityCheck?: EqualityCheck<T>
): Finisher<T, boolean>function contains<T>(
item: T,
equalityCheck?: EqualityCheck<T>
): Finisher<T, boolean>Arguments
item:T- The item to check if the source containsequalityCheck:EqualityCheck<T>(optional) - A function that can determine if an item of the source and the provided item are equal. Defaults to the===operator.
Example
const result = query(
from([1, 2, 3]),
contains(3)
)
console.log(result)const result = query(
from([1, 2, 3]),
contains(3)
)
console.log(result)Output
truetrueCount
Creates a Finisher that determines how many items in the source Iterable satisfy the given predicate. If there is no predicate given, the operator will simply return the number of total items in the source.
function count<T>(
predicate?: (item: T) => boolean
): Finisher<T, number>function count<T>(
predicate?: (item: T) => boolean
): Finisher<T, number>Arguments
predicate:(item: T) => boolean(optional) - A function that tests an item if it should be counted
Example
const result = query(
from([1, 2, 3]),
count(value => value < 3)
)
console.log(result)const result = query(
from([1, 2, 3]),
count(value => value < 3)
)
console.log(result)Output
22ElementAt
Creates a Finisher that returns the item at the specified index of the source Iterable.
function elementAt<T>(index: number): Finisher<T, T>function elementAt<T>(index: number): Finisher<T, T>Arguments
index:number- The zero based index of the desired item
Example
const result = query(
from(["apple", "banana", "lemon"]),
elementAt(2)
)
console.log(result)const result = query(
from(["apple", "banana", "lemon"]),
elementAt(2)
)
console.log(result)Output
lemonlemonFirst
Creates a Finisher that returns the first item that matches the given predicate. If no predicate is specified, it simply returns the first item of the source Iterable. Throws an error if there were no matching items in the source.
function first<T>(predicate?: (item: T) => boolean): Finisher<T, T>function first<T>(predicate?: (item: T) => boolean): Finisher<T, T>Arguments
predicate:(item: T) => boolean(optional) - A function that tests an item
Example
const result = query(
from(['apple', 'banana', 'lemon']),
first()
)
console.log(result)const result = query(
from(['apple', 'banana', 'lemon']),
first()
)
console.log(result)Output
appleappleFirstOrNull
Creates a Finisher that returns the first item that matches the given predicate. If no predicate is specified, it simply returns the first item of the source Iterable. If the source is empty or has no elements that match the predicate, null is returned.
function firstOrNull<T>(
predicate?: (item: T) => boolean
): Finisher<T, T | null>function firstOrNull<T>(
predicate?: (item: T) => boolean
): Finisher<T, T | null>Arguments
predicate:(item: T) => boolean(optional) - A function that tests an item
Example
const result = query(
from([1, 2, 3]),
firstOrNull(value => value > 1)
)const result = query(
from([1, 2, 3]),
firstOrNull(value => value > 1)
)Output
22Join
Creates a Finisher that joins the items of the source Iterable<string> into a single string, where the items are separated by the given separator.
function join(separator?: string): Finisher<string, string>function join(separator?: string): Finisher<string, string>Arguments
separator:string(optional) - A string that will be used to separate the items of the source. If omitted, a comma will be used.
Example
const result = query(
from(['apple', 'banana', 'lemon']),
join(', ')
)
console.log(result)const result = query(
from(['apple', 'banana', 'lemon']),
join(', ')
)
console.log(result)Output
apple, banana, lemonapple, banana, lemonLast
Creates a Finisher that returns the last element that matches the given predicate. If no predicate is specified it simply returns the last element of the source Iterable. Throws an error if the source is empty or no elements match the predicate.
function last<T>(predicate?: (item: T) => boolean): Finisher<T, T>function last<T>(predicate?: (item: T) => boolean): Finisher<T, T>Arguments
predicate:(item: T) => boolean(optional) - A function that tests an item
Example
const result = query(
from([1, 2, 3]),
last(value => value < 3)
)
console.log(result)const result = query(
from([1, 2, 3]),
last(value => value < 3)
)
console.log(result)Output
22LastOrNull
Creates a Finisher that returns the last element that matches the given predicate. If no predicate is specified it simply returns the last element of the source Iterable. If the source is empty or has no elements that match the predicate, null is returned.
function lastOrNull<T>(
predicate?: (item: T) => boolean
): Finisher<T, T | null>function lastOrNull<T>(
predicate?: (item: T) => boolean
): Finisher<T, T | null>Arguments
predicate:(item: T) => boolean(optional) - A function that tests an item
Example
const result = query(
from([1, 2, 3]),
lastOrNull(value => value < 1)
)
console.log(result)const result = query(
from([1, 2, 3]),
lastOrNull(value => value < 1)
)
console.log(result)Output
nullnullMax
Creates a Finisher that returns the greatest element of the source Iterable.
function max<T>(
comparator: Comparator<T> = defaultComparator
): Finisher<T, T | null>function max<T>(
comparator: Comparator<T> = defaultComparator
): Finisher<T, T | null>Arguments
comparator:Comparator<R>- An optional function that compares two values of the source. Defaults to a function that returns-1ifleft < right,0ifleft === right,1otherwise. For more details see Comparator.
Example
const result = query(
from([3, 5, 6, 2]),
max()
)
console.log(result)const result = query(
from([3, 5, 6, 2]),
max()
)
console.log(result)Output
66MaxBy
Creates a Finisher that returns the element that has the greatest value selected by the given selector.
function maxBy<T, R>(
selector: (item: T) => R,
comparator: Comparator<R> = defaultComparator
): Finisher<T, T | null>function maxBy<T, R>(
selector: (item: T) => R,
comparator: Comparator<R> = defaultComparator
): Finisher<T, T | null>Arguments
selector:(item: T) => R- A function that transforms an item to a value that can be used to compare the items by.comparator:Comparator<R>- An optional function that compares the selected values. Defaults to a function that returns-1ifleft < right,0ifleft === right,1otherwise. For more details see Comparator.
Example
const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
maxBy(person => person.age)
)
console.log(result)const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
maxBy(person => person.age)
)
console.log(result)Output
{ name: 'John', age: 25 }{ name: 'John', age: 25 }MaxOf
Creates a Finisher that returns the greatest value selected by the given selector.
function maxOf<T, R>(
selector: (item: T) => R,
comparator: Comparator<R> = defaultComparator
): Finisher<T, R | null>function maxOf<T, R>(
selector: (item: T) => R,
comparator: Comparator<R> = defaultComparator
): Finisher<T, R | null>Arguments
selector:(item: T) => R- A function that transforms an item to a value that will be compared.comparator:Comparator<R>- An optional function that compares the selected values. Defaults to a function that returns-1ifleft < right,0ifleft === right,1otherwise. For more details see Comparator.
Example
const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
maxOf(person => person.age)
)
console.log(result)const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
maxOf(person => person.age)
)
console.log(result)Output
2525Min
Creates a Finisher that returns the smallest element of the source Iterable.
function min<T>(
comparator: Comparator<T> = defaultComparator
): Finisher<T, T | null>function min<T>(
comparator: Comparator<T> = defaultComparator
): Finisher<T, T | null>Arguments
comparator:Comparator<R>- An optional function that compares two values of the source. Defaults to a function that returns-1ifleft < right,0ifleft === right,1otherwise. For more details see Comparator.
Example
const result = query(
from([3, 5, 6, 2]),
min()
)
console.log(result)const result = query(
from([3, 5, 6, 2]),
min()
)
console.log(result)Output
22MinBy
Creates a Finisher that returns the item that has the smallest value selected by the given selector.
function minBy<T, R>(
selector: (item: T) => R,
comparator: Comparator<R> = defaultComparator
): Finisher<T, T | null>function minBy<T, R>(
selector: (item: T) => R,
comparator: Comparator<R> = defaultComparator
): Finisher<T, T | null>Arguments
selector:(item: T) => R- A function that transforms an item to a value that can be used to compare the items by.comparator:Comparator<R>- An optional function that compares the selected values. Defaults to a function that returns-1ifleft < right,0ifleft === right,1otherwise. For more details see Comparator.
Example
const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
minBy(person => person.age)
)
console.log(result)const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
minBy(person => person.age)
)
console.log(result)Output
{ name: 'Jane', age: 20 }{ name: 'Jane', age: 20 }MinOf
Creates a Finisher that returns the smallest value selected by the given selector.
function minOf<T, R>(
selector: (item: T) => R,
comparator: Comparator<R> = defaultComparator
): Finisher<T, R | null>function minOf<T, R>(
selector: (item: T) => R,
comparator: Comparator<R> = defaultComparator
): Finisher<T, R | null>Arguments
selector:(item: T) => R- A function that transforms an item to a value that will be compared.comparator:Comparator<R>- An optional function that compares the selected values. Defaults to a function that returns-1ifleft < right,0ifleft === right,1otherwise. For more details see Comparator.
Example
const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
minOf(person => person.age)
)
console.log(result)const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
minOf(person => person.age)
)
console.log(result)Output
2020SequenceEquals
Creates a Finisher that checks if the given Iterable has the same items as the source Iterable.
function sequenceEquals<T>(
other: Iterable<T>,
equalityCheck: EqualityCheck<T> = defaultEqualityCheck
): Finisher<T, boolean>function sequenceEquals<T>(
other: Iterable<T>,
equalityCheck: EqualityCheck<T> = defaultEqualityCheck
): Finisher<T, boolean>Arguments
other:Iterable<T>- The otherIterableto compare the source withequalityCheck:EqualityCheck<T>(optional) - A function that can determine if an item of the source and an item of the other is considered equal. Defaults to the===operator.
Example
const result = query(
from([1, 2, 3]),
sequenceEquals([1, 2, 3])
)
console.log(result)const result = query(
from([1, 2, 3]),
sequenceEquals([1, 2, 3])
)
console.log(result)Output
truetrueSingle
Creates a Finisher that returns the only item that matches the given predicate. If no predicate is specified it simply returns the only item of the source Iterable. If there are multiple items in the source and no predicate is given, or there are multiple items that match the predicate, an error is thrown. The function also throws an error if the source is empty or has no matching item.
function single<T>(predicate?: (item: T) => boolean): Finisher<T, T>function single<T>(predicate?: (item: T) => boolean): Finisher<T, T>Arguments
predicate:(item: T) => boolean(optional) - A function that tests an item
Example
const result = query(
from([1, 2, 3]),
single(value => value > 2)
)
console.log(result)const result = query(
from([1, 2, 3]),
single(value => value > 2)
)
console.log(result)Output
33SingleOrNull
Creates a Finisher that returns the only item that matches the given predicate. If no predicate is specified it simply returns the only item of the source Iterable. If there are multiple items in the source and no predicate is given, or there are multiple items that match the predicate, an error is thrown. If the source is empty or has no matching items to the predicate, it returns null.
function singleOrNull<T>(
predicate?: (item: T) => boolean
): Finisher<T, T | null>function singleOrNull<T>(
predicate?: (item: T) => boolean
): Finisher<T, T | null>Arguments
predicate:(item: T) => boolean(optional) - A function that tests an item
Example
const result = query(
from([1, 2, 3]),
singleOrNull(value => value > 2)
)
console.log(result)const result = query(
from([1, 2, 3]),
singleOrNull(value => value > 2)
)
console.log(result)Output
33Sum
Creates a Finisher that calculates the sum of items in the source Iterable<number>.
function sum(): Finisher<number, number>function sum(): Finisher<number, number>Example
const result = query(
from([1, 2, 3]),
sum()
)
console.log(result)const result = query(
from([1, 2, 3]),
sum()
)
console.log(result)Output
66SumOf
Creates a Finisher that calculates the sum of the values mapped from the source sequence by the given selector.
function sumOf<T>(selector: (item: T) => number): Finisher<T, number>function sumOf<T>(selector: (item: T) => number): Finisher<T, number>Arguments
selector:(item: T) => number- A function that transforms an item to a number that will be used to calculate the sum
Example
const people = [
{
name: "John",
dogs: 2
},
{
name: "Jane",
dogs: 1
},
]
const result = query(
from(people),
sumOf(person => person.dogs)
)
console.log(result)const people = [
{
name: "John",
dogs: 2
},
{
name: "Jane",
dogs: 1
},
]
const result = query(
from(people),
sumOf(person => person.dogs)
)
console.log(result)Output
33ToArray
Creates a Finisher that collects the items of the source Iterable into an array. It can be used to materialize / get a snapshot of the result of a query.
function toArray<T>(): Finisher<T, T[]>function toArray<T>(): Finisher<T, T[]>Example
const result = query(
from([1, 2, 3]),
select(value => valuevalue),
toArray()
)
console.log(result)const result = query(
from([1, 2, 3]),
select(value => valuevalue),
toArray()
)
console.log(result)Output
[1, 4, 9][1, 4, 9]ToMap
Creates a Finisher that collects the items of the source Iterable into a map where the keys for each element are selected by the given key selector.
Overload #1
function toMap<T, K>(keySelector: (item: T) => K): Finisher<T, Map<K, T>>function toMap<T, K>(keySelector: (item: T) => K): Finisher<T, Map<K, T>>Arguments
keySelector:(item: T) => K- A function that determines what key will belong to an item in the resulting map
Example
const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
toMap(person => person.name)
)
for (const [key, value] of result) {
console.log([key, value])
}const people = [
{
name: 'John',
age: 25
},
{
name: 'Jane',
age: 20
},
]
const result = query(
from(people),
toMap(person => person.name)
)
for (const [key, value] of result) {
console.log([key, value])
}Output
['John', { name: 'John', age: 25 }]
['Jane', { name: 'Jane', age: 20 }]['John', { name: 'John', age: 25 }]
['Jane', { name: 'Jane', age: 20 }]Overload #2
Values are selected by a given value selector.
function toMap<T, K, V>(
keySelector: (item: T) => K,
valueSelector: (item: T) => V
): Finisher<T, Map<K, V>>function toMap<T, K, V>(
keySelector: (item: T) => K,
valueSelector: (item: T) => V
): Finisher<T, Map<K, V>>Arguments
keySelector:(item: T) => K- A function that determines what key will belong to an item in the resulting mapvalueSelector:(item: T) => V- A function that transforms an item from the sourceIterableto an item in the resulting map
Example
const people = [
{
name: "John",
age: 25
},
{
name: "Jane",
age: 20
},
]
const result = query(
from(people),
toMap(person => person.name, person => person.age)
)
for (const [key, value] of result) {
console.log([key, value])
}const people = [
{
name: "John",
age: 25
},
{
name: "Jane",
age: 20
},
]
const result = query(
from(people),
toMap(person => person.name, person => person.age)
)
for (const [key, value] of result) {
console.log([key, value])
}Output
['John', 25]
['Jane', 20]['John', 25]
['Jane', 20]ToSet
Creates a Finisher that collects the items of the source Iterable and returns it as a Set.
function toSet<T>(): Finisher<T, Set<T>>function toSet<T>(): Finisher<T, Set<T>>Example
const result = query(
from([1, 2, 6, 1, 4, 4]),
toSet()
)
for (const item of result) {
console.log(item)
}const result = query(
from([1, 2, 6, 1, 4, 4]),
toSet()
)
for (const item of result) {
console.log(item)
}Output
1
2
6
41
2
6
4