使用node.js和lazy时了解EOF

发布于 2024-11-28 01:31:19 字数 792 浏览 2 评论 0原文

我有一个用 CoffeeScript 编写的例程,在 node.js 中运行,从 Jade 文件中读取行。它看起来像这样:

each_line = (file, callback) ->
  last_line = null

  lazy = Lazy(fs.createReadStream(file)).lines.map(String).filter (line) -> 
    not (
      # Filter the not interesting rows in the top of the file
      /^html$/i.test(line) or 
      /^\s+body$/i.test(line) or 
      /^\s+\/\/\s+Generator.*$/i.test(line)
      )

  lazy.forEach (line) ->
    # We only emit those lines that are whole lines, multilines will become joined
    if /^\t/g.test(line)
      last_line += line.replace(/\t/g, '').replace(/(\r\n|\n|\r)/gm," ")
    else
      callback(last_line) if last_line
      last_line = line.replace(/(\r\n|\n|\r)/gm," ")

由于所有这些都是异步运行的,所以我需要知道 EOF,以便我可以在整个迭代完成后执行操作。有人有什么建议吗?

I have a routine written in CoffeeScript, running in node.js that reads lines from a Jade-file. It looks like this:

each_line = (file, callback) ->
  last_line = null

  lazy = Lazy(fs.createReadStream(file)).lines.map(String).filter (line) -> 
    not (
      # Filter the not interesting rows in the top of the file
      /^html$/i.test(line) or 
      /^\s+body$/i.test(line) or 
      /^\s+\/\/\s+Generator.*$/i.test(line)
      )

  lazy.forEach (line) ->
    # We only emit those lines that are whole lines, multilines will become joined
    if /^\t/g.test(line)
      last_line += line.replace(/\t/g, '').replace(/(\r\n|\n|\r)/gm," ")
    else
      callback(last_line) if last_line
      last_line = line.replace(/(\r\n|\n|\r)/gm," ")

Since all this runs async, I need to know EOF so that I can perform operations after the whole iteration is done. Does anyone have any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

守望孤独 2024-12-05 01:31:19

我会先说我自己没有使用过 Lazy,但我只是看了文档。

我有点期待 Lazy 发出“结束”事件,但我无法判断是否会发出“结束”事件,无论哪种方式,您都可以将回调直接绑定到读取流上的结束事件。

each_line = (file, callback, eof_callback) ->
  last_line = null

  stream = fs.createReadStream file

  stream.on 'end', eof_callback

  lazy = Lazy(stream).lines.map(String).filter (line) -> 
    not (
      # Filter the not interesting rows in the top of the file
      /^html$/i.test(line) or 
      /^\s+body$/i.test(line) or 
      /^\s+\/\/\s+Generator.*$/i.test(line)
      )

  lazy.forEach (line) ->
    # We only emit those lines that are whole lines, multilines will become joined
    if /^\t/g.test(line)
      last_line += line.replace(/\t/g, '').replace(/(\r\n|\n|\r)/gm," ")
    else
      callback(last_line) if last_line
      last_line = line.replace(/(\r\n|\n|\r)/gm," ")

I will preface this by saying that I haven't used Lazy myself, but I just looked at the docs.

I was somewhat expecting Lazy to emit an 'end' event, but I can't tell if it does, either way you can bind a callback directly to the end event on the read stream.

each_line = (file, callback, eof_callback) ->
  last_line = null

  stream = fs.createReadStream file

  stream.on 'end', eof_callback

  lazy = Lazy(stream).lines.map(String).filter (line) -> 
    not (
      # Filter the not interesting rows in the top of the file
      /^html$/i.test(line) or 
      /^\s+body$/i.test(line) or 
      /^\s+\/\/\s+Generator.*$/i.test(line)
      )

  lazy.forEach (line) ->
    # We only emit those lines that are whole lines, multilines will become joined
    if /^\t/g.test(line)
      last_line += line.replace(/\t/g, '').replace(/(\r\n|\n|\r)/gm," ")
    else
      callback(last_line) if last_line
      last_line = line.replace(/(\r\n|\n|\r)/gm," ")
酒解孤独 2024-12-05 01:31:19

我相信 Lazy 会在创建的原始 Lazy 对象上发出“结束”事件:

Lazy = require('lazy')
fs = require('fs')

readLines = (filename, next) ->
  lazy = new Lazy(fs.createReadStream(filename))
  lazy.on 'end', ->
    if next then next null
  lazy.lines.forEach (line) ->
    console.log 'Read line:', line.toString()

readLines process.argv[1], ->
  console.log 'All done.'

I believe Lazy will emit an 'end' event on the original Lazy object that was created:

Lazy = require('lazy')
fs = require('fs')

readLines = (filename, next) ->
  lazy = new Lazy(fs.createReadStream(filename))
  lazy.on 'end', ->
    if next then next null
  lazy.lines.forEach (line) ->
    console.log 'Read line:', line.toString()

readLines process.argv[1], ->
  console.log 'All done.'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文