2016-07-20 8 views
1

私はオブジェクトのストリーム(objectMode: true)を持っていて、それを同じサイズの配列にチャンクしたいので、配列を受け入れる別の関数にパイプすることができます。オブジェクトのストリームをチャンクする方法は?

あなたは知っていますか:私はバッファ用ではなく、オブジェクトのためにそれを達成するように見える以下のモジュールが見つかりましたオブジェクトストリームのためにこれを行うことができるモジュールのrは明らかな単純なDIYソリューションですか?

答えて

2

私は仕事に次のが見つかりました:

function ItemCollector (chunkSize) { 

    this._buffer = []; 
    this._chunkSize = chunkSize; 

    stream.Transform.call(this, { objectMode: true }); 
} 

util.inherits(ItemCollector, stream.Transform); 

ItemCollector.prototype._transform = function (chunk, encoding, done) { 

    if (this._buffer.length == this._chunkSize) { 

    this.push(this._buffer); 
    this._buffer = []; 
    } 

    this._buffer.push(chunk); 

    done(); 
}; 

ItemCollector.prototype._flush = function() { 

    this.push(this._buffer); 
}; 

次のように使用します。

objectStream.pipe(new ItemCollector(10)).pipe(otherFunction) 
関連する問題