skipWhile.js 842 B

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