skip.js 723 B

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