CSV Generate 示例
回调
The parser receive a string and return an array inside a user-provided callback.
执行脚本命令 samples/callback.js
var generate = require('csv-generate');
generate({seed: 1, columns: 2, length: 2}, function(err, output){
output.should.eql('OMH,ONKCHhJmjadoA\nD,GeACHiN');
});
流
// node samples/stream.js
var generate = require('csv-generate');
var data = []
var generator = generate({seed: 1, objectMode: true, columns: 2, length: 2});
generator.on('readable', function(){
while(d = generator.read()){
data.push(d);
}
});
generator.on('error', function(err){
console.log(err);
});
generator.on('end', function(){
data.should.eql([ [ 'OMH', 'ONKCHhJmjadoA' ],[ 'D', 'GeACHiN' ] ]);
});
管道
One usefull function part of the Stream API is pipe to interact between multiple streams. You may use this function to pipe a stream.Readable string source to a stream.Writable object destination. The next example available as node samples/pipe.js read the file, parse its content and transform it.
// node samples/pipe.js
var generate = require('csv-generate');
var generator = generate({columns: ['int', 'bool'], length: 2});
generator.pipe(process.stdout);