https://project.mdnd-it.cc/work_packages/94
This commit is contained in:
2025-08-23 04:25:28 +02:00
parent 725516ad6c
commit 19cfa031d0
25823 changed files with 1095587 additions and 2801760 deletions
+47
View File
@@ -0,0 +1,47 @@
'use strict';
const {Transform} = require('stream');
const alwaysTrue = () => true;
class TakeWhile extends Transform {
constructor(options) {
super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
this._condition = alwaysTrue;
if (options) {
'condition' in options && (this._condition = options.condition);
}
}
_transform(chunk, encoding, callback) {
const result = this._condition.call(this, chunk);
if (result && typeof result.then == 'function') {
result.then(
flag => {
if (flag) {
this.push(chunk);
} else {
this._transform = this._doNothing;
}
callback(null);
},
error => callback(error)
);
} else {
if (result) {
this.push(chunk);
} else {
this._transform = this._doNothing;
}
callback(null);
}
}
_doNothing(chunk, encoding, callback) {
callback(null);
}
static make(condition) {
return new TakeWhile(typeof condition == 'object' ? condition : {condition});
}
}
TakeWhile.make.Constructor = TakeWhile;
module.exports = TakeWhile.make;