StreamArray.js 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. 'use strict';
  2. const StreamBase = require('./StreamBase');
  3. const withParser = require('../utils/withParser');
  4. class StreamArray extends StreamBase {
  5. static make(options) {
  6. return new StreamArray(options);
  7. }
  8. static withParser(options) {
  9. return withParser(StreamArray.make, options);
  10. }
  11. constructor(options) {
  12. super(options);
  13. this._level = 1;
  14. this._counter = 0;
  15. }
  16. _wait(chunk, _, callback) {
  17. // first chunk should open an array
  18. if (chunk.name !== 'startArray') {
  19. return callback(new Error('Top-level object should be an array.'));
  20. }
  21. this._transform = this._filter;
  22. return this._transform(chunk, _, callback);
  23. }
  24. _push(discard) {
  25. if (this._assembler.current.length) {
  26. if (discard) {
  27. ++this._counter;
  28. this._assembler.current.pop();
  29. } else {
  30. this.push({key: this._counter++, value: this._assembler.current.pop()});
  31. }
  32. }
  33. }
  34. }
  35. StreamArray.streamArray = StreamArray.make;
  36. StreamArray.make.Constructor = StreamArray;
  37. module.exports = StreamArray;