Skip to content

Instantly share code, notes, and snippets.

@slonoed
Created December 16, 2016 13:28
Show Gist options
  • Save slonoed/294cf31871466ab9e22fc75749d75942 to your computer and use it in GitHub Desktop.
Save slonoed/294cf31871466ab9e22fc75749d75942 to your computer and use it in GitHub Desktop.
Stream. Transform string chunks to data chunks.
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