Created
January 18, 2018 15:01
-
-
Save dgoguerra/2957f9d8e330e95cc24fadb8b897440e to your computer and use it in GitHub Desktop.
Stringify an object stream into an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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