Wednesday, 10 May 2017

why is the delimiter being included by the split function of this node script?

My solution to the Stream Adventure 'Lines' problem is similar to the official solution, but for some reason my version is retaining new line characters that shouldn't be included.

 expected:
      'riverrun, past eve and adam\'s, from swerve of shore to bend \nOF BAY, BRINGS US BY A COMMODIUS VICUS OF RECIRCULATION BACK TO    \nhowth castle and environs. \n\nsir tristram, violer d\'amores, fr\'over the short sea, had passen- \nCORE REARRIVED FROM NORTH ARMORICA ON THIS SIDE THE SCRAGGY\nisthmus of europe minor to wielderfight his penisolate war: nor    \nHAD TOPSAWYER\'S ROCKS BY THE STREAM OCONEE EXAGGERATED THEMSELSE   \nto laurens county\'s gorgios while they went doublin their mumper   \nALL THE TIME: NOR AVOICE FROM AFIRE BELLOWSED MISHE MISHE TO   \ntauftauf thuartpeatrick: not yet, though venissoon after, had a   \nKIDSCAD BUTTENDED A BLAND OLD ISAAC: NOT YET, THOUGH ALL\'S FAIR IN    \nvanessy, were sosie sesthers wroth with twone nathandjoe. rot a   \nPECK OF PA\'S MALT HAD JHEM OR SHEN BREWED BY ARCLIGHT AND RORY    \nend to the regginbrow was to be seen ringsome on the aquaface.'
    actual:
      'riverrun, past eve and adam\'s, from swerve of shore to bend OF BAY, BRINGS US BY A COMMODIUS VICUS OF RECIRCULATION BACK TO    howth castle and environs. sir tristram, violer d\'amores, fr\'over the short sea, had passen- CORE REARRIVED FROM NORTH ARMORICA ON THIS SIDE THE SCRAGGYisthmus of europe minor to wielderfight his penisolate war: nor    HAD TOPSAWYER\'S ROCKS BY THE STREAM OCONEE EXAGGERATED THEMSELSE   to laurens county\'s gorgios while they went doublin their mumper   ALL THE TIME: NOR AVOICE FROM AFIRE BELLOWSED MISHE MISHE TO   tauftauf thuartpeatrick: not yet, though venissoon after, had a   KIDSCAD BUTTENDED A BLAND OLD ISAAC: NOT YET, THOUGH ALL\'S FAIR IN    vanessy, were sosie sesthers wroth with twone nathandjoe. rot a   PECK OF PA\'S MALT HAD JHEM OR SHEN BREWED BY ARCLIGHT AND RORY    end to the regginbrow was to be seen ringsome on the aquaface.'

Here's my solution:

var through = require('through2');
var split = require('split');
var uppercase = false;
var stream = through(function(buf, _, next){
    if (uppercase){
    this.push(buf.toString().toUpperCase());
    }
    else {
    this.push(buf.toString().toLowerCase());
    }
    uppercase = !uppercase;    
    next();
});

process.stdin.pipe(split()).pipe(stream).pipe(process.stdout);

And the official solution:

var through = require('through2');
var split = require('split');

var lineCount = 0;
var tr = through(function (buf, _, next) {
    var line = buf.toString();
    this.push(lineCount % 2 === 0
        ? line.toLowerCase() + '\n'
        : line.toUpperCase() + '\n'
    );
    lineCount ++;
    next();
});
process.stdin
    .pipe(split())
    .pipe(tr)
    .pipe(process.stdout)
;

Can someone explain why mine is retaining the delimiter? Thanks.



via David J.

No comments:

Post a Comment