Skip to content

Commit

Permalink
fix(take): do not consume one extra element when finishing (#224)
Browse files Browse the repository at this point in the history
* fix(take): do not consume an extra element when enough number of elements are taken

* fix ci failure
  • Loading branch information
crossing committed Jul 23, 2023
1 parent 7d5d329 commit 089898c
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/ops/take.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ function takeSync<T>(iterable: Iterable<T>, count: number): Iterable<T> {
finished: boolean;
return {
next(): IteratorResult<T> {
finished = finished || index++ >= count;
if (!finished) {
const a = i.next();
finished = a.done || index++ >= count;
finished = !!a.done;
if (!finished) {
return a;
}
Expand All @@ -61,11 +62,12 @@ function takeAsync<T>(
finished: boolean;
return {
next(): Promise<IteratorResult<T>> {
finished = finished || index++ >= count;
if (finished) {
return Promise.resolve({value: undefined, done: true});
}
return i.next().then((a) => {
finished = a.done || index++ >= count;
finished = !!a.done;
return finished ? {value: undefined, done: true} : a;
});
}
Expand Down

0 comments on commit 089898c

Please sign in to comment.