Created
April 21, 2020 22:50
-
-
Save bdombro/125171d9aba4890126ad8ab49487ed58 to your computer and use it in GitHub Desktop.
Easy stream filters for, say, file read streams/pipes
This file contains 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
/** | |
* Easy stream filters for, say, file read streams/pipes | |
*/ | |
const stream = require('stream') | |
class StreamFilter extends stream.Transform { | |
constructor(filterCallback) { | |
super({ | |
readableObjectMode: true, | |
writableObjectMode: true | |
}) | |
this.filterCallback = filterCallback; | |
} | |
_transform(chunk, encoding, next) { | |
if (this.filterCallback(chunk)) { | |
return next(null, chunk) | |
} | |
next() | |
} | |
} | |
const streamFilter = filterCallback => new StreamFilter(filterCallback); | |
module.exports = streamFilter; | |
// function test () { | |
// const fs = require('fs'); | |
// const csv = require('csv'); | |
// | |
// const StreamFilter = require('./src/lib/StreamFilter'); | |
// | |
// const src = __dirname + "/src.tsv"; | |
// const dest = __dirname + "/test.csv"; | |
// | |
// const srcStream = fs.createReadStream(src); | |
// const destStream = fs.createWriteStream(dest); | |
// | |
// srcStream | |
// .on('end', () => console.log('CSV file successfully processed')) | |
// // .on('data', (row) => console.log(row)) | |
// .pipe(csv.parse({delimiter: "\t"})) | |
// .pipe(streamFilter(cols => cols[0].includes('magicKeyword'))) | |
// .pipe(csv.transform(cols => ( | |
// [ | |
// cols[3], | |
// cols[1], | |
// cols[2], | |
// ] | |
// ))) | |
// .pipe(csv.stringify()) | |
// .pipe(destStream) | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment