Guest User

Untitled

a guest
Jun 18th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. import { Transform } from "stream";
  2.  
  3. export default class LineSplitter extends Transform {
  4. private forEachLine: (l: string) => any;
  5. private buffer: string | undefined = "";
  6.  
  7. constructor(forEachLine = l => l, encoding = "utf8") {
  8. super({ encoding });
  9. this.forEachLine = forEachLine;
  10. }
  11.  
  12. _transform(chunk, encoding, done) {
  13. this.buffer += chunk.toString();
  14. let lines = this.buffer!.split(/\r?\n/);
  15. this.buffer = lines.pop();
  16. lines.forEach(line => this.push(this.forEachLine(line)));
  17. done();
  18. }
  19.  
  20. _flush(done) {
  21. if (this.buffer) this.push(this.forEachLine(this.buffer));
  22. this.buffer = "";
  23. done();
  24. }
  25. }
Add Comment
Please, Sign In to add comment