CSV Stringify 示例
回调
This source code for this example is available in "./samples/api.callback.js".
The stringifier receive an array and return a string inside a user-provided callback. This example is available with the command node samples/callback.js.
var stringify = require('csv-stringify');
input = [ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ];
stringify(input, function(err, output){
output.should.eql('1,2,3,4\na,b,c,d');
});
流
This source code for this example is available in "./samples/api.stream.js".
// node samples/stream.js
var stringify = require('csv-stringify');
data = '';
stringifier = stringify({delimiter: ':'})
stringifier.on('readable', function(){
while(row = stringifier.read()){
data += row;
}
});
stringifier.on('error', function(err){
consol.log(err.message);
});
stringifier.on('finish', function(){
data.should.eql(
"root:x:0:0:root:/root:/bin/bash\n" +
"someone:x:1022:1022:a funny cat:/home/someone:/bin/bash"
);
});
stringifier.write([ 'root','x','0','0','root','/root','/bin/bash' ]);
stringifier.write([ 'someone','x','1022','1022','a funny cat','/home/someone','/bin/bash' ]);
stringifier.end();
管道
This source code for this example is available in "./samples/api.pipe.js".
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 array or object source to a stream.Writable string destination. The next example available as node samples/pipe.js generate records, stringify them and print them to stdout.
var stringify = require('csv-stringify');
var generate = require('csv-generate');
generator = generate({objectMode: true, seed: 1, headers: 2});
stringifier = stringify();
generator.pipe(stringifier).pipe(process.stdout);
使用 "header" 选项
This source code for this example is available in "./samples/options.header.js". Run it with the command node ./samples/options.header.js.
var stringify = require('../lib');
var generate = require('csv-generate');
var generator = generate({objectMode: true, seed: 1, headers: 2});
var columns = {
year: 'birthYear',
phone: 'phone'
};
var stringifier = stringify({ header: true, columns: columns });
generator.pipe(stringifier).pipe(process.stdout);
The output on the console will start with:
birthYear,phone
OMH,ONKCHhJmjadoA
KB,dmiM
B,LF