【问题标题】:How to conditionally pipe output in node.js?如何有条件地在 node.js 中管道输出?
【发布时间】:2014-11-04 20:45:02
【问题描述】:

只有当我的第一个流满足某些条件时,我才希望我的流通过管道传输到另一个流。

es = require('event-stream');

var checkBuffer = function() {
    return es.map(function(file, cb) {
        if (!file.isBuffer()) {
            cb();
        } else {
            cb(null, file);
        }
    });
};

module.exports = function() { checkBuffer.pipe( mysteryFoo()); return checkBuffer; };

只有当管道传输到 checkBuffer 的文件通过 isBuffer 时,我才想管道到神秘富,否则我不希望这个管道发生。

将其输出通过管道传送到该模块的函数是gulp.src('/some-dest/**')

谢谢!

【问题讨论】:

    标签: javascript node.js stream pipe gulp


    【解决方案1】:

    看看gulp-if,它在后台使用gulp-match 来检查黑胶唱片文件是否符合条件。

    从 gulp-match 中提取的用法示例:

    var gulpmatch = require('gulp-match');
    var map = require('map-stream');
    
    var condition = true; // TODO: add business logic here
    
    vinylfs.src('path/to/file.js')
        .pipe(map(function (file, cb) {
            var match = gulpmatch(file, condition);
            if (match) {
              // it matched, do stuff
            }
            cb(null, file);
        }));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-05
      • 2020-11-01
      • 1970-01-01
      相关资源
      最近更新 更多