我正在浏览nodesschool的流探险workshopper。我要写一个程序来对这个过程进行管道化。Stdin流到使用through2模块创建的流,然后将其管道到process.stdout。使用through2模块创建的流将把输入流字符转换为大写,并将其管道到process.stdout。下面是我的源代码:
var through = require('through2');
var write = function(buffer,encoding,next){
var data = buffer.toString();
this.push(data.toUpperCase() + 'n');
next();
}
var end = function (done){
done();
}
var stream = through(write,end);
process.stdin.pipe(stream).pipe(process.stdout);
问题是,每发送到流中的第8个字块之后,就会打印一个额外的换行符。下面是workshopper执行的验证的3个测试输出:
测试1:TAP version 13
# (anonymous)
ok 1 stream-adventure verify YOURFILE.js
not ok 2 should be equal
---
operator: equal
expected:
'METALKINDnKORVENnOSIRIANnTHE ERGONnTYTHONIANnLAND OF FICTION BEINGSnSIRIANnKRAALnTHE FLOODnJADONDANn'
actual:
'METALKINDnKORVENnOSIRIANnTHE ERGONnTYTHONIANnLAND OF FICTION BEINGSnSIRIANnKRAALnnTHE FLOODnnJADONDANnn'
...
ok 3 successful exit code
1..3
# tests 3
# pass 2
# fail 1
#########################################
### YOUR SOLUTION IS NOT CORRECT! ###
#########################################
测试2:TAP version 13
# (anonymous)
ok 1 stream-adventure verify YOURFILE.js
not ok 2 should be equal
---
operator: equal
expected:
'THE BEASTnANDROZANI TREEnGAZTAKnRAXACORICOFALLAPATORIANnMAYFLYnSTIGORAXnTHE TRICKSTERnMEGARAnMENTORnHAND OF OMEGAn'
actual:
'THE BEASTnANDROZANI TREEnGAZTAKnRAXACORICOFALLAPATORIANnMAYFLYnSTIGORAXnTHE TRICKSTERnMEGARAnMENTORnnHAND OF OMEGAnn'
...
ok 3 successful exit code
1..3
# tests 3
# pass 2
# fail 1
#########################################
### YOUR SOLUTION IS NOT CORRECT! ###
#########################################
测试3:TAP version 13
# (anonymous)
ok 1 stream-adventure verify YOURFILE.js
not ok 2 should be equal
---
operator: equal
expected:
'QETESHnPELUSHInLURMANnEVEnATRAXInFLESHKINDnDALEKnGEE-JEE FLYnHEADLESS MONKnTHE DESTROYERn'
actual:
'QETESHnPELUSHInLURMANnEVEnATRAXInFLESHKINDnDALEKnGEE-JEE FLYnnHEADLESS MONKnnTHE DESTROYERnn'
...
ok 3 successful exit code
1..3
# tests 3
# pass 2
# fail 1
#########################################
### YOUR SOLUTION IS NOT CORRECT! ###
#########################################
这个神秘的额外换行符的原因是什么?我如何修复它?
在代码的第4行添加额外的换行符。要解决这个问题,您可以将第4行更改为:
this.push(data.toUpperCase() + 'n');
:
this.push(data.toUpperCase());
并且当您在终端
中键入此命令时,stream-adventure课程将打印它们的参考解决方案。stream-adventure solution
不要添加'n',它已经在流中了。我猜你得到的缓冲区不止一行,所以'n'是随机添加的。