-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing-6.3.js
46 lines (39 loc) · 1.37 KB
/
listing-6.3.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
//
// Example that imports a CSV file, fixes date/time values in each row and exporting a new CSV file.
// Date/times are fixed by encoding in UTC so that the correct timezone is preserved.
//
// This example uses Data-Forge.
//
"use strict";
const moment = require('moment');
const dataForge = require('data-forge');
const importDateFormat = "YYYY-MM-DD HH:mm";
const inputFileName = "./data/surveys.csv";
const outputFileName = "./output/surveys-with-fixed-dates-using-data-forge.csv";
function parseDate (inputDate, timezoneOffset) {
return moment(inputDate, importDateFormat).utcOffset(timezoneOffset).toDate();
}
function transformRow (inputRow) {
const outputRow = Object.assign({}, inputRow);
outputRow.start_datetime = parseDate(inputRow.start_datetime, inputRow.timezone);
outputRow.end_datetime = parseDate(inputRow.end_datetime, inputRow.timezone);
return outputRow;
}
function transformData (inputDataFrame) {
return inputDataFrame.select(transformRow);
}
dataForge.readFile(inputFileName)
.parseCSV()
.then(inputDataFrame => {
const outputDataFrame = transformData(inputDataFrame);
return outputDataFrame
.asCSV()
.writeFile(outputFileName);
})
.then(() => {
console.log("Done!");
})
.catch(err => {
console.error("Error!");
console.error(err && err.stack || err);
});