invalid
invalid

Reputation: 1515

How can I read files by word (divided with space and return) like `/\s+/g` with fs.createReadStream in Node.js?

I want to read file below and parse it by space and return with Node.js.

5
12 45 67 34 56

And I can do it with fs.readFileSync("path","utf8").split(/\s+/g);.

But how can I do this with fs.createReadStream? If I use stream, word or number will be broken like below.

123 43 5762748834

chunk1 123 43 57
chunk2 62748834

Upvotes: 1

Views: 336

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30715

You can implement a Transform stream for this purpose if you wish, in this example we'll just number the words, but you can do whatever you need in the _transform function. One point, you may have to deal with chunks breaking across words, this should be easy to do however.

const fs = require("fs");
const stream = require("stream");
const Transform = stream.Transform;

let wordIndex = 0;

class streamSplitter extends Transform {

    _transform(chunk, enc, done) {
        let words = chunk.toString("utf8").split(/\s+/g);
        // Process words here. In this example we'll simply attach a number to each word.
        let processedChunk = words.map(word => `Word #${++wordIndex}: ${word}`).join("\n");
        done(null, processedChunk);
    }
}

fs.createReadStream('inputfile.txt')
    .pipe(new streamSplitter())
    .pipe(fs.createWriteStream('outputfile.txt'))

Upvotes: 2

Related Questions