DefinitelyTyped/csv-parse/csv-parse-tests.ts
2016-06-03 00:05:08 +09:00

65 lines
1.9 KiB
TypeScript

/// <reference path="csv-parse.d.ts" />
import parse = require('csv-parse');
function callbackAPITest() {
var input = '#Welcome\n"1","2","3","4"\n"a","b","c","d"';
parse(input, {comment: '#'}, function(err, output){
output.should.eql([ [ '1', '2', '3', '4' ], [ 'a', 'b', 'c', 'd' ] ]);
});
}
function streamAPITest() {
var output:any = [];
// Create the parser
var parser = parse({delimiter: ':'});
var record: any;
// Use the writable stream api
parser.on('readable', function(){
while(record = parser.read()){
output.push(record);
}
});
// Catch any error
parser.on('error', function(err: any){
console.log(err.message);
});
// When we are done, test that the parsed output matched what expected
parser.on('finish', function(){
output.should.eql([
[ 'root','x','0','0','root','/root','/bin/bash' ],
[ 'someone','x','1022','1022','a funny cat','/home/someone','/bin/bash' ]
]);
});
// Now that setup is done, write data to the stream
parser.write("root:x:0:0:root:/root:/bin/bash\n");
parser.write("someone:x:1022:1022:a funny cat:/home/someone:/bin/bash\n");
// Close the readable stream
parser.end();
}
import fs = require('fs');
function pipeFunctionTest() {
var transform = require('stream-transform');
var output:any = [];
var parser = parse({delimiter: ':'})
var input = fs.createReadStream('/etc/passwd');
var transformer = transform(function(record: any[], callback: any){
setTimeout(function(){
callback(null, record.join(' ')+'\n');
}, 500);
}, {parallel: 10});
input.pipe(parser).pipe(transformer).pipe(process.stdout);
}
import parseSync = require('csv-parse/lib/sync');
function syncApiTest() {
var input = '"key_1","key_2"\n"value 1","value 2"';
var records = parseSync(input, {columns: true});
records.should.eql([{ key_1: 'value 1', key_2: 'value 2' }]);
}