Can this be done by the transform class and how does it look like

34 views
Skip to first unread message

Roelof Wobben

unread,
Jan 16, 2015, 11:21:51 AM1/16/15
to nod...@googlegroups.com
Hello,

As a solution I made this :

~~~

var through = require('through');
var tr = through(function write(data) {
        this.queue(data.toString().toUpperCase())
    },
    function end () {
        this.queue(null)
    });
process.stdin.pipe(tr).pipe(process.stdout);

~~~

Now I have read that in node 0.10 there is a transform class who can do the same.

Does anyone have a tip if this can be done and how I can do this ?

Roelof

Luca Morandini

unread,
Jan 17, 2015, 1:19:13 PM1/17/15
to nod...@googlegroups.com
On 17/01/15 03:21, Roelof Wobben wrote:
>
> Does anyone have a tip if this can be done and how I can do this ?

var fs= require("fs");
var stream= require("stream");
var util= require("util");
var inFile= fs.createReadStream("in.txt", {encoding: "utf8"});
var outFile= fs.createWriteStream("out.txt");

var UpperTransform = function(options) {
if (!(this instanceof UpperTransform)) {
return new UpperTransform(options);
}
stream.Transform.call(this, options);
};
util.inherits(UpperTransform, stream.Transform);

UpperTransform.prototype._transform = function(line, enc, done) {
this.push(line.toUpperCase());
done();
};

var upper = new UpperTransform({ objectMode: true });

upper.on("error", function(err) {
console.log(err)
});

inFile.pipe(upper).pipe(outFile);

Enjoy !

Luca Morandini
Data Architect - AURIN project
Melbourne eResearch Group
Department of Computing and Information Systems
University of Melbourne




Roelof Wobben

unread,
Jan 18, 2015, 4:51:43 AM1/18/15
to nod...@googlegroups.com, lmora...@ieee.org


Op zaterdag 17 januari 2015 19:19:13 UTC+1 schreef Luca Morandini:
Thanks, now I see why plugin like through are easier to use and to understand.

Roelof
 

Floby

unread,
Jan 19, 2015, 4:54:10 AM1/19/15
to nod...@googlegroups.com
oh that can be done like this to


function UpperTransform() {
  var toUpper = stream.Transform();
  toUpper._transform = function (chunk, encoding, done) {
    this.push(chunk.toString().toUpperCase());
    done();
  }
  return toUpper;
Reply all
Reply to author
Forward
0 new messages