StreamObject.js 1.1 KB

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