Skip to content

Instantly share code, notes, and snippets.

@dgoguerra
Created January 18, 2018 15:01
Show Gist options
  • Save dgoguerra/2957f9d8e330e95cc24fadb8b897440e to your computer and use it in GitHub Desktop.
Save dgoguerra/2957f9d8e330e95cc24fadb8b897440e to your computer and use it in GitHub Desktop.
Stringify an object stream into an array
var through = require('through2');
function stringifyIntoArray() {
var isFirstElem = true;
return through.obj(function(data, enc, next) {
if (isFirstElem) {
this.push('[');
isFirstElem = false;
} else {
this.push(',');
}
this.push(JSON.stringify(data));
next();
}, function(next) {
this.push(']');
next();
});
}
objStream.pipe(stringifyIntoArray()).pipe(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment