True if the element is in the sequence. Checked with ===.
from([2, 1, 5]).contains(1)
// => true
from([{ a: '1' }]).contains({ a: '1' })
// => false (strict equality. use any with a custom search instead.)
Get all distinct elements of the sequence using strict equality. First value wins.
from([1, 2, 2, 3]).distinct()
// => [1, 2, 3]
Get all distinct elements of the sequence, mapping each element to a key that will be used for strict equality. First value wins.
from([10, 15, 20, 25, 30]).distinct(x => Math.trunc(x / 10))
// => [10, 20, 30]
Get the first element in the sequence. Will throw if empty! Use firstOrDefault if no throw is wanted.
from([2, 1, 5]).first()
// => 2
from([]).first()
// => throws
Get the first element in the sequence that matches a condition. Will throw if empty! Use firstOrDefault if no throw is wanted.
from([2, 1, 5]).first(x => x >= 4)
// => 5
from([2, 1, 5]).first(x => x < 0)
// => throws
Get the first element in the sequence. If empty, returns undefined.
from([2, 1, 5]).firstOrDefault()
// => 2
from([]).firstOrDefault()
// => undefined
Get the first element in the sequence that matches a condition. If empty or no matches, returns undefined.
from([2, 1, 5]).firstOrDefault(x => x >= 4)
// => 5
from([2, 1, 5]).firstOrDefault(x => x < 0)
// => undefined
Project each element to an iterable/array, then flatten the result
from([[1, 2], [3, 4]]).flat(x => [...x, 0])
// => [1, 2, 0, 3, 4, 0]
Flatten the sequence
from([[1, 2], [3, 4]]).flat()
// => [1, 2, 3, 4]
Project each element to get a key, and group all items by that key.
from([10, 15, 20]).groupBy(x => Math.trunc(x / 10))
.map(g => ({ key: g.key, values: Array.from(g) }))
// => [{ key: 1, values: [10, 15] },
// { key: 2, values: [20] }]
Project each element to get a key, and group all items, each projected onto another type.
from([10, 15, 20]).groupBy(x => Math.trunc(x / 10), x => x.toString())
.map(g => ({ key: g.key, values: Array.from(g) }))
// => [{ key: 1, values: ['10', '15'] },
// { key: 2, values: ['20'] }]
Correlates the elements of two sequences based on matching keys, and groups everything in the other table.
const appleTypes = [
{ name: 'green apple', id: 5 },
{ name: 'red apple', id: 2 },
{ name: 'yellow apple', id: 10 }
];
const apples = [
{ name: 'golden delicious', type: 10 },
{ name: 'granny smith', type: 5 },
{ name: 'pink lady', type: 2 },
{ name: 'fuji', type: 2 },
{ name: 'unknown', type: 999 }
];
from(appleTypes).groupJoin(
apples,
type => type.id,
apple => apple.type,
(type, apples) => `${type.name}: ${apples.map(a => a.name).joinString(', ')}`
);
// => [ 'green apple: granny smith',
// 'red apple: pink lady, fuji',
// 'yellow apple: golden delicious' ]
Correlates the elements of two sequences based on matching keys. An inner join.
const appleTypes = [
{ name: 'green apple', id: 5 },
{ name: 'red apple', id: 2 },
{ name: 'yellow apple', id: 10 }
];
const apples = [
{ name: 'golden delicious', type: 10 },
{ name: 'granny smith', type: 5 },
{ name: 'pink lady', type: 2 },
{ name: 'fuji', type: 2 },
{ name: 'unknown', type: 999 }
];
from(apples).join(
appleTypes,
apple => apple.type,
type => type.id,
(apple, type) => `${apple.name}: ${type.name}`
)
// => [ 'golden delicious: yellow apple',
// 'granny smith: green apple',
// 'pink lady: red apple',
// 'fuji: red apple' ]
Get the last element in the sequence. Will throw if empty! Use lastOrDefault if no throw is wanted.
from([2, 1, 5]).last()
// => 5
from([]).last()
// => throws
Get the last element in the sequence that matches a condition. Will throw if empty! Use lastOrDefault if no throw is wanted.
from([2, 1, 5]).last(x => x < 4)
// => 1
from([]).last(x => x < 0)
// => throws
Get the last element in the sequence. If empty, returns undefined.
from([2, 1, 5]).lastOrDefault()
// => 5
from([]).lastOrDefault()
// => undefined
Get the last element in the sequence that matches a condition. If empty or no matches, returns undefined.
from([2, 1, 5]).lastOrDefault(x => x < 4)
// => 1
from([2, 1, 5]).lastOrDefault(x => x < 0)
// => undefined
Sort the array in ascending order of the selector
from([4, 1, 10]).orderBy(x => x)
// => [1, 4, 10]
Sort the array in ascending order of the selector, with a custom comparer
// Sort alphabetically, ignoring case.
from(['A xylophone', 'a frog', 'a zoo']).orderBy(x => x, new Intl.Collator('en', { sensitivity: 'base' }).compare)
// => ['a frog', 'A xylophone', 'a zoo']
Sort the array in descending order of the selector
from([4, 1, 10]).orderByDescending(x => x)
// => [10, 4, 1]
Sort the array in descending order of the selector, with a custom comparer
// Sort reverse alphabetically, ignoring case.
from(['A xylophone', 'a frog', 'a zoo']).orderByDescending(x => x, new Intl.Collator('en', { sensitivity: 'base' }).compare)
// => ['a zoo', 'A xylophone', 'a frog']
Get the only element in the sequence. Will throw if empty or more than one element! Use singleOrDefault if no throw is wanted.
from([2]).single()
// => 2
from([2, 1, 5]).single()
// => throws
from([]).single()
// => throws
Get the only element in the sequence that matches a condition. Will throw if empty or more than one element! Use singleOrDefault if no throw is wanted.
from([2, 1, 5]).single(x => x >= 4)
// => 5
from([2, 1, 5]).single(x => x >= 1)
// => throws
Get the only element in the sequence. Returns undefined if empty or more than one element.
from([2]).singleOrDefault()
// => 2
from([2, 1, 5]).singleOrDefault()
// => undefined
Get the only element in the sequence that matches a condition. Returns undefined if empty or more than one element.
from([2, 1, 5]).singleOrDefault(x => x >= 4)
// => 5
from([2, 1, 5]).singleOrDefault(x => x >= 1)
// => undefined
Convert this sequence into a Set
from([2, 1, 1, 5]).toSet()
// => new Set([2, 1, 5])
Map each element and convert the resulting sequence into a set
from([2, 1, 1, 5]).toSet(x => x + 1)
// => new Set([3, 2, 6])
Filters with a TS type assertion ('is'), narrowing to that type
function isNumber(x: number | string): x is number {
return typeof x === 'number';
}
from(['string', 12, 'str']).where(isNumber)
// => [12]
// sequence is now Sequence<number>
Filters with and narrows to a type
from(['string', 12, 'str']).where<number>(x => typeof x === 'number')
// => [12]
// sequence is now Sequence<number>
note! must use generic type! otherwise this overload is not used, and the type is not narrowed.
Filters with a predicate that must return a truthy value
from([5, 10, 20]).where(x => x >= 8)
// => [10, 20]
A sequence of iterable elements