stream: add filter method to readable · nodejs/node@77c1868 · GitHub
Skip to content

Commit 77c1868

Browse files
benjamingrdanielleadams
authored andcommitted
stream: add filter method to readable
This continues the work in #40815 to make streams compatible with upcoming ECMAScript language features. It adds an experimental `filter` api to streams and tests/docs for it. See https://github.com/tc39/proposal-iterator-helpers/ Co-Authored-By: Robert Nagy <ronagy@icloud.com> PR-URL: #41354 Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 7618b55 commit 77c1868

3 files changed

Lines changed: 172 additions & 0 deletions

File tree

doc/api/stream.md

Lines changed: 49 additions & 0 deletions

lib/internal/streams/operators.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ async function * map(fn, options) {
147147
}
148148
}
149149

150+
async function * filter(fn, options) {
151+
if (typeof fn !== 'function') {
152+
throw (new ERR_INVALID_ARG_TYPE(
153+
'fn', ['Function', 'AsyncFunction'], this));
154+
}
155+
async function filterFn(value, options) {
156+
if (await fn(value, options)) {
157+
return value;
158+
}
159+
return kEmpty;
160+
}
161+
yield* this.map(filterFn, options);
162+
}
150163
module.exports = {
151164
map,
165+
filter
152166
};
Lines changed: 109 additions & 0 deletions

0 commit comments

Comments
 (0)