Stringer.js 747 B

1234567891011121314151617181920212223242526272829
  1. 'use strict';
  2. const {Transform} = require('stream');
  3. class JsonlStringer extends Transform {
  4. static make(options) {
  5. return new JsonlStringer(options);
  6. }
  7. constructor(options) {
  8. super(Object.assign({}, options, {writableObjectMode: true, readableObjectMode: false}));
  9. this._replacer = options && options.replacer;
  10. }
  11. _transform(chunk, _, callback) {
  12. this.push(JSON.stringify(chunk, this._replacer));
  13. this._transform = this._nextTransform;
  14. callback(null);
  15. }
  16. _nextTransform(chunk, _, callback) {
  17. this.push('\n' + JSON.stringify(chunk, this._replacer));
  18. callback(null);
  19. }
  20. }
  21. JsonlStringer.stringer = JsonlStringer.make;
  22. JsonlStringer.make.Constructor = JsonlStringer;
  23. module.exports = JsonlStringer;