-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
listing-7.8.js
61 lines (51 loc) · 2 KB
/
listing-7.8.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
'use strict';
const stream = require('stream');
const openJsonInputStream = require('./toolkit/open-json-input-stream.js');
const openJsonOutputStream = require('./toolkit/open-json-output-stream.js');
const inputFilePath = "./data/weather-stations.json";
const outputFilePath = "./output/weather-stations-ftransformed.json";
//
// Convert the temperature for a single record.
// Converts from 'tenths of degress celcius' to 'degrees celcius'.
//
// https://earthscience.stackexchange.com/questions/5015/what-is-celsius-degrees-to-tenths
//
function transformRow (inputRow) {
// Your code here to transform a row of data.
const outputRow = Object.assign({}, inputRow); // Clone record, prefer not to modify source data.
if (typeof(outputRow.MinTemp) === "number") {
outputRow.MinTemp /= 10;
}
else {
outputRow.MinTemp = undefined;
}
if (typeof(outputRow.MaxTemp) === "number") {
outputRow.MaxTemp /= 10;
}
else {
outputRow.MaxTemp = undefined;
}
return outputRow;
};
//
// Create a stream that converts the temperature for all records that pass through the stream.
//
function convertTemperatureStream () {
const transformStream = new stream.Transform({ objectMode: true }); // Create a bidirectional stream in 'object mode'.
transformStream._transform = (inputChunk, encoding, callback) => { // Callback to execute on chunks that are input.
var outputChunk = transformRow(inputChunk); // Transform the chunk.
transformStream.push(outputChunk); // Pass the converted chunk to the output stream.
callback();
};
return transformStream;
};
//
// Use Node.js streams to pipe the content of one JSON file to another, transforming the data on the way through.
//
openJsonInputStream(inputFilePath)
.pipe(convertTemperatureStream())
.pipe(openJsonOutputStream(outputFilePath))
.on("error", err => {
console.error("An error occurred while transforming the JSON file.");
console.error(err);
});