Skip to content

Instantly share code, notes, and snippets.

@keevitaja
Created June 26, 2016 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keevitaja/247f814289929b89ae169f18b4e53d10 to your computer and use it in GitHub Desktop.
Save keevitaja/247f814289929b89ae169f18b4e53d10 to your computer and use it in GitHub Desktop.
"use strict";
/*
data.txt:
1
2
3
4
5
6
*/
const fs = require('fs');
const Transformer = require('stream').Transform;
let stream = fs.createReadStream('data.txt');
let gt3 = (chunk)=> {
let partial = [];
let str = chunk.toString().split(/\n/);
for (let i = 0, len = str.length; i < len; i++) {
if (str[i] > 3) {
partial.push(str[i]);
}
}
return new Buffer(partial.join('\n'));
};
class Input extends Transformer {
_transform(chunk, encoding) {
this.push(gt3(chunk));
}
}
const input = new Input();
stream.pipe(input);
input.on('data', (chunk)=> {
console.log(chunk.toString());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment