skipWhile.js 827 B

123456789101112131415161718192021222324252627282930
  1. const {Transform} = require('stream');
  2. const alwaysFalse = () => false;
  3. class SkipWhile extends Transform {
  4. constructor(options) {
  5. super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: true}));
  6. this._condition = alwaysFalse;
  7. if (options) {
  8. 'condition' in options && (this._condition = options.condition);
  9. }
  10. }
  11. _transform(chunk, encoding, callback) {
  12. if (!this._condition.call(this, chunk)) {
  13. this._transform = this._passThrough;
  14. this.push(chunk);
  15. }
  16. callback(null);
  17. }
  18. _passThrough(chunk, encoding, callback) {
  19. this.push(chunk);
  20. callback(null);
  21. }
  22. static make(condition) {
  23. return new SkipWhile(typeof condition == 'object' ? condition : {condition});
  24. }
  25. }
  26. SkipWhile.make.Constructor = SkipWhile;
  27. module.exports = SkipWhile.make;