Commit fbb3931 Shil Sinha
committed
1 parent 36eb702 commit fbb3931 Copy full SHA for fbb3931
File tree 1 file changed +22
-1
lines changed
1 file changed +22
-1
lines changed Original file line number Diff line number Diff line change
1
+ const assert = require ( 'assert' ) ;
1
2
const sift = require ( 'sift' ) . default ;
2
3
3
4
/**
@@ -15,6 +16,17 @@ async function *filterIt(filter, iter) {
15
16
}
16
17
}
17
18
19
+ async function * limitIt ( limit , iter ) {
20
+ let count = 0 ;
21
+ for await ( const item of iter ) {
22
+ yield item ;
23
+
24
+ if ( ++ count === limit ) {
25
+ break
26
+ }
27
+ }
28
+ }
29
+
18
30
/**
19
31
* @template T
20
32
* @implements AsyncIterable<T>
@@ -61,6 +73,15 @@ class DecoratedIterable {
61
73
}
62
74
return result ;
63
75
}
76
+
77
+ /**
78
+ * @param {number } limit
79
+ * @return {DecoratedIterable<T> }
80
+ */
81
+ limit ( limit ) {
82
+ assert ( limit && typeof limit === 'number' , '"limit" must be a positive integer' ) ;
83
+ return new DecoratedIterable ( limitIt ( limit , this ) ) ;
84
+ }
64
85
}
65
86
66
87
module . exports = {
@@ -73,4 +94,4 @@ module.exports = {
73
94
decorateIt ( iter ) {
74
95
return new DecoratedIterable ( iter ) ;
75
96
}
76
- } ;
97
+ } ;
You can’t perform that action at this time.
0 commit comments