Last change
on this file since bdd6491 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
677 bytes
|
Rev | Line | |
---|
[6a3a178] | 1 | 'use strict';
|
---|
| 2 | const {Transform} = require('stream');
|
---|
| 3 |
|
---|
| 4 | class ObjectTransform extends Transform {
|
---|
| 5 | constructor() {
|
---|
| 6 | super({
|
---|
| 7 | objectMode: true
|
---|
| 8 | });
|
---|
| 9 | }
|
---|
| 10 | }
|
---|
| 11 |
|
---|
| 12 | class FilterStream extends ObjectTransform {
|
---|
| 13 | constructor(filter) {
|
---|
| 14 | super();
|
---|
| 15 | this._filter = filter;
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | _transform(data, encoding, callback) {
|
---|
| 19 | if (this._filter(data)) {
|
---|
| 20 | this.push(data);
|
---|
| 21 | }
|
---|
| 22 |
|
---|
| 23 | callback();
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | class UniqueStream extends ObjectTransform {
|
---|
| 28 | constructor() {
|
---|
| 29 | super();
|
---|
| 30 | this._pushed = new Set();
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | _transform(data, encoding, callback) {
|
---|
| 34 | if (!this._pushed.has(data)) {
|
---|
| 35 | this.push(data);
|
---|
| 36 | this._pushed.add(data);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | callback();
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | module.exports = {
|
---|
| 44 | FilterStream,
|
---|
| 45 | UniqueStream
|
---|
| 46 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.