UNPKG

1.64 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.transformFilterSync = exports.transformFilter = void 0;
4const stream_1 = require("stream");
5/**
6 * Note, that currently it's NOT concurrent! (concurrency = 1)
7 * So, it's recommended to use transformMap instead, that is both concurrent and has
8 * filtering feature by default.
9 */
10function transformFilter(predicate, opt = {}) {
11 let index = 0;
12 return new stream_1.Transform({
13 objectMode: true,
14 ...opt,
15 async transform(chunk, _encoding, cb) {
16 try {
17 if (await predicate(chunk, index++)) {
18 cb(null, chunk); // pass through
19 }
20 else {
21 cb(); // signal that we've finished processing, but emit no output here
22 }
23 }
24 catch (err) {
25 cb(err);
26 }
27 },
28 });
29}
30exports.transformFilter = transformFilter;
31/**
32 * Sync version of `transformFilter`
33 */
34function transformFilterSync(predicate, opt = {}) {
35 let index = 0;
36 return new stream_1.Transform({
37 objectMode: true,
38 ...opt,
39 async transform(chunk, _encoding, cb) {
40 try {
41 if (predicate(chunk, index++)) {
42 cb(null, chunk); // pass through
43 }
44 else {
45 cb(); // signal that we've finished processing, but emit no output here
46 }
47 }
48 catch (err) {
49 cb(err);
50 }
51 },
52 });
53}
54exports.transformFilterSync = transformFilterSync;