Skip to content

Commit af09236

Browse files
author
Shil Sinha
committed
feat(iterate): add function to limit iteration
1 parent 36eb702 commit af09236

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

iterUtils.js

+21-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ async function *filterIt(filter, iter) {
1515
}
1616
}
1717

18+
async function *limitIt(limit, iter) {
19+
let count = 0;
20+
for await (const item of iter) {
21+
if (count === limit) {
22+
break
23+
}
24+
25+
count++;
26+
yield item;
27+
}
28+
}
29+
1830
/**
1931
* @template T
2032
* @implements AsyncIterable<T>
@@ -61,6 +73,14 @@ class DecoratedIterable {
6173
}
6274
return result;
6375
}
76+
77+
/**
78+
* @param {number} limit
79+
* @return {DecoratedIterable<T>}
80+
*/
81+
limit(limit) {
82+
return new DecoratedIterable(limitIt(limit, this));
83+
}
6484
}
6585

6686
module.exports = {
@@ -73,4 +93,4 @@ module.exports = {
7393
decorateIt(iter) {
7494
return new DecoratedIterable(iter);
7595
}
76-
};
96+
};

0 commit comments

Comments
 (0)