Created
December 16, 2016 13:28
-
-
Save slonoed/294cf31871466ab9e22fc75749d75942 to your computer and use it in GitHub Desktop.
Stream. Transform string chunks to data chunks.
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
class LineStream extends Transform { | |
constructor(...args) { | |
super(...args); | |
this.buffer = ''; | |
} | |
_transform(chunk, encoding, callback) { | |
const lines = chunk.toString().split('\n'); | |
lines[0] = this.buffer + lines[0]; | |
this.buffer = lines.length > 1 ? | |
lines[lines.length - 1] : | |
''; | |
lines.forEach(l => this.push(l)); | |
callback(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment