Skip to content

Commit d188c9f

Browse files
committed
docs: comment stream samples
1 parent 710ef7b commit d188c9f

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

packages/csv-generate/samples/api.stream.js

+4
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ import { generate } from 'csv-generate'
33
import assert from 'assert'
44

55
const records = []
6+
// Initialize the generator
67
generate({
78
seed: 1,
89
objectMode: true,
910
columns: 2,
1011
length: 2
1112
})
13+
// Use the readable stream api to consume generated records
1214
.on('readable', function(){
1315
let record
1416
while(record = this.read()){
1517
records.push(record)
1618
}
1719
})
20+
// Catch any error
1821
.on('error', function(err){
1922
console.error(err)
2023
})
24+
// Test that the generated records matched the expected records
2125
.on('end', function(){
2226
assert.deepEqual(records, [
2327
[ 'OMH', 'ONKCHhJmjadoA' ],

packages/csv-parse/samples/api.stream.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import assert from 'assert';
33
import { parse } from 'csv-parse';
44

55
const records = [];
6-
// Create the parser
6+
// Initialize the parser
77
const parser = parse({
88
delimiter: ':'
99
});
10-
// Use the readable stream api
10+
// Use the readable stream api to consume records
1111
parser.on('readable', function(){
1212
let record;
1313
while ((record = parser.read()) !== null) {
@@ -18,7 +18,7 @@ parser.on('readable', function(){
1818
parser.on('error', function(err){
1919
console.error(err.message);
2020
});
21-
// When we are done, test that the parsed records matched what expected
21+
// Test that the parsed records matched the expected records
2222
parser.on('end', function(){
2323
assert.deepStrictEqual(
2424
records,

packages/csv-stringify/samples/api.stream.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,31 @@ import { stringify } from 'csv-stringify';
33
import assert from 'assert';
44

55
const data = [];
6+
// Initialize the stringifier
67
const stringifier = stringify({
78
delimiter: ':'
89
});
10+
// Use the readable stream api to consume CSV data
911
stringifier.on('readable', function(){
1012
let row;
1113
while((row = stringifier.read()) !== null){
1214
data.push(row);
1315
}
1416
});
17+
// Catch any error
1518
stringifier.on('error', function(err){
1619
console.error(err.message);
1720
});
21+
// When finished, validate the CSV output with the expected value
1822
stringifier.on('finish', function(){
1923
assert.equal(
2024
data.join(''),
2125
"root:x:0:0:root:/root:/bin/bash\n" +
2226
"someone:x:1022:1022::/home/someone:/bin/bash\n"
2327
);
2428
});
29+
// Write records to the stream
2530
stringifier.write([ 'root','x','0','0','root','/root','/bin/bash' ]);
2631
stringifier.write([ 'someone','x','1022','1022','','/home/someone','/bin/bash' ]);
32+
// Close the writable stream
2733
stringifier.end();

packages/csv/samples/stream.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,38 +11,46 @@ const transformer = csv.transform(function(data){
1111
});
1212
const stringifier = csv.stringify();
1313

14+
// Read generated CSV data and send it to the parser
1415
generator.on('readable', function(){
1516
let data; while(data = generator.read()){
1617
parser.write(data);
1718
}
1819
});
20+
// When generation is over, close the parser
1921
generator.on('end', function(){
2022
parser.end()
2123
});
2224

25+
// Read parsed records and send them to the transformer
2326
parser.on('readable', function(){
2427
let data; while(data = parser.read()){
2528
transformer.write(data);
2629
}
2730
});
31+
// When parsing is over, close the transformer
2832
parser.on('end', function(){
2933
transformer.end()
3034
});
3135

36+
// Read transformed records and send them to the stringifier
3237
transformer.on('readable', function(){
3338
let data; while(data = transformer.read()){
3439
stringifier.write(data);
3540
}
3641
});
42+
// When transformation is over, close the stringifier
3743
transformer.on('end', function(){
3844
stringifier.end();
3945
});
4046

47+
// Read CSV data and print it to stdout
4148
stringifier.on('readable', function(){
4249
let data; while(data = stringifier.read()){
4350
process.stdout.write(data);
4451
}
4552
});
53+
// When stringifying is over, print a summary to stderr
4654
generator.on('end', function(){
47-
process.stdout.write('=> ' + i + ' records\n');
55+
process.stderr.write('=> ' + i + ' records\n');
4856
});

packages/stream-transform/samples/api.stream.js

+6
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,30 @@ import { transform } from 'stream-transform';
33
import assert from 'assert';
44

55
const output = [];
6+
// Initialize the transformer
67
const transformer = transform(function(data){
78
data.push(data.shift());
89
return data;
910
});
11+
// Use the readable stream api to consume transformed records
1012
transformer.on('readable', function(){
1113
let row; while((row = transformer.read()) !== null){
1214
output.push(row);
1315
}
1416
});
17+
// Catch any error
1518
transformer.on('error', function(err){
1619
console.error(err.message);
1720
});
21+
// When finished, validate the records with the expected value
1822
transformer.on('finish', function(){
1923
assert.deepEqual(output, [
2024
[ '2', '3', '4', '1' ],
2125
[ 'b', 'c', 'd', 'a' ]
2226
]);
2327
});
28+
// Write records to the stream
2429
transformer.write(['1','2','3','4']);
2530
transformer.write(['a','b','c','d']);
31+
// Close the writable stream
2632
transformer.end();

0 commit comments

Comments
 (0)