Skip to content

Commit 18d9b25

Browse files
committed
Fixes to scripts so they all consistently pass a single object through the stream at a time.
1 parent de55bcd commit 18d9b25

5 files changed

+10
-23
lines changed

listing-5.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,13 @@ function transformRow (inputRow) {
3636
return outputRow;
3737
};
3838

39-
//
40-
// Transform a data set (in this case a chunk of rows).
41-
//
42-
function transformData (inputData) {
43-
// Your code here to transform a batch of rows.
44-
return inputData.map(transformRow);
45-
};
46-
4739
//
4840
// Create a stream that converts the temperature for all records that pass through the stream.
4941
//
5042
function convertTemperatureStream () {
5143
const transformStream = new stream.Transform({ objectMode: true }); // Create a bidirectional stream in 'object mode'.
5244
transformStream._transform = (inputChunk, encoding, callback) => { // Callback to execute on chunks that are input.
53-
var outputChunk = transformData(inputChunk); // Transform the chunk.
45+
var outputChunk = transformRow(inputChunk); // Transform the chunk.
5446
transformStream.push(outputChunk); // Pass the converted chunk to the output stream.
5547
callback();
5648
};

listing-8.js

+2-9
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,14 @@ function transformRow (inputRow) {
3636
return outputRow;
3737
};
3838

39-
//
40-
// Transform a data set (in this case a chunk of rows).
41-
//
42-
function transformData (inputData) {
43-
// Your code here to transform a batch of rows.
44-
return inputData.map(transformRow);
45-
};
46-
4739
//
4840
// Create a stream that converts the temperature for all records that pass through the stream.
4941
//
5042
function convertTemperatureStream () {
5143
const transformStream = new stream.Transform({ objectMode: true }); // Create a bidirectional stream in 'object mode'.
5244
transformStream._transform = (inputChunk, encoding, callback) => { // Callback to execute on chunks that are input.
53-
var outputChunk = transformData(inputChunk); // Transform the chunk.
45+
console.log(inputChunk); //fio:
46+
var outputChunk = transformRow(inputChunk); // Transform the chunk.
5447
transformStream.push(outputChunk); // Pass the converted chunk to the output stream.
5548
callback();
5649
};

toolkit/open-csv-input-stream.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ function openCsvInputStream (inputFilePath) {
2121
skipEmptyLines: true,
2222

2323
step: (results) => { // Handles incoming rows of CSV data.
24-
csvInputStream.push(results.data); // Push results as they are streamed from the file.
24+
for (let row of results.data) {
25+
csvInputStream.push(row); // Push results as they are streamed from the file.
26+
}
2527
},
2628

2729
complete: () => { // File read operation has completed.

toolkit/open-csv-output-stream.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function openCsvOutputStream (outputFilePath) {
1414

1515
const csvOutputStream = new stream.Writable({ objectMode: true }); // Create stream for writing data records, note that 'object mode' is enabled.
1616
csvOutputStream._write = (chunk, encoding, callback) => { // Handle writes to the stream.
17-
var outputCSV = papaparse.unparse(chunk, {
17+
var outputCSV = papaparse.unparse([chunk], {
1818
header: firstOutput
1919
});
2020
fileOutputStream.write(outputCSV + '\n');

toolkit/open-json-output-stream.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ function openJsonOutputStream (outputFilePath) {
1616
const jsonOutputStream = new stream.Writable({ objectMode: true });
1717
jsonOutputStream._write = (chunk, encoding, callback) => {
1818
if (numRecords > 0) {
19-
fileOutputStream.write(",");
19+
fileOutputStream.write(",\n");
2020
}
2121

2222
// Output a single row of a JSON array.
23-
const jsonData = JSON.stringify(curObject);
23+
const jsonData = JSON.stringify(chunk);
2424
fileOutputStream.write(jsonData);
25-
numRecords += chunk.length;
25+
++numRecords;
2626
callback();
2727
};
2828

0 commit comments

Comments
 (0)