在转换流中使用 Node.js readline

krl*_*krl 7 stream readline node.js

我们有一个巨大的文本文件,我们想要使用流逐行操作它。

有没有办法在转换流中使用 Node.js readline模块?例如使整个文本使用大写字母(逐行处理)?

rob*_*lep 1

event-stream可能更合适。它可以分割行上的输入并以各种方式转换这些行(+更多)。

例如,将从 stdin 读取的所有内容都大写:

const es = require('event-stream');

process.stdin
  .pipe(es.split())                              // split lines
  .pipe(es.mapSync(data => data.toUpperCase()))  // uppercase the line
  .pipe(es.join('\n'))                           // add a newline again
  .pipe(process.stdout);                         // write to stdout
Run Code Online (Sandbox Code Playgroud)

  • 谨防!该软件包已存档(可能不再维护),并且根据 https://github.com/dominictarr/event-stream/issues/115 存在严重的安全风险 (2认同)