skip.js 738 B

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