-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve typings for the filter operator (#20)
Co-authored-by: Felix Becker <[email protected]>
- Loading branch information
1 parent
b30ed1c
commit 3a70457
Showing
1 changed file
with
7 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
export class FilterIterator<T> implements Iterator<T> { | ||
constructor(private source: Iterator<T>, private predicate: (element: T) => boolean) {} | ||
export class FilterIterator<T, V extends T = T> implements Iterator<T> { | ||
constructor( | ||
private source: Iterator<T>, | ||
private predicate: ((element: T) => element is V) | ((element: T) => boolean) | ||
) {} | ||
|
||
next(): IteratorResult<T> { | ||
next(): IteratorResult<V> { | ||
let result: IteratorResult<T> | ||
// Skip elements until predicate returns true | ||
do { | ||
result = this.source.next() | ||
} while (!result.done && !this.predicate(result.value)) | ||
return result | ||
return result as IteratorResult<V> | ||
} | ||
} |