Transforming text to uppercase

混江龙づ霸主 提交于 2019-12-25 09:48:18

问题


I've found an example of a transform stream here that transforms text to uppercase:

Upper.prototype._transform = function (chunk, enc, cb) {
  var upperChunk = chunk.toString().toUpperCase();
  this.push(upperChunk);
  cb();
};

Is my understanding correct that such approach might lead to errors when multi-byte encoding is used because a character bytes can be split between two chunks?

Wouldn't it be better to gather all chunks and then on end transform them? Something like this (not sure it'll work)

var body = [];
request.on('data', function(chunk) {
  body.push(chunk);
}).on('end', function() {
  this.push(Buffer.concat(body).toString());
});

Is transform function not used in this case?

来源:https://stackoverflow.com/questions/40358449/transforming-text-to-uppercase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!