Add concat
iterable utility
This commit is contained in:
parent
028b4f162b
commit
7c7f7ee5a0
3 changed files with 98 additions and 5 deletions
|
@ -2,6 +2,7 @@
|
|||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
/* eslint-disable max-classes-per-file */
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
|
||||
export function isIterable(value: unknown): value is Iterable<unknown> {
|
||||
return (
|
||||
|
@ -28,6 +29,22 @@ export function size(iterable: Iterable<unknown>): number {
|
|||
return result;
|
||||
}
|
||||
|
||||
export function concat<T>(
|
||||
...iterables: ReadonlyArray<Iterable<T>>
|
||||
): Iterable<T> {
|
||||
return new ConcatIterable(iterables);
|
||||
}
|
||||
|
||||
class ConcatIterable<T> implements Iterable<T> {
|
||||
constructor(private readonly iterables: ReadonlyArray<Iterable<T>>) {}
|
||||
|
||||
*[Symbol.iterator](): Iterator<T> {
|
||||
for (const iterable of this.iterables) {
|
||||
yield* iterable;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function filter<T, S extends T>(
|
||||
iterable: Iterable<T>,
|
||||
predicate: (value: T) => value is S
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue