-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listing-6.11.js
56 lines (48 loc) · 1.46 KB
/
listing-6.11.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
//
// Split a data file out to multiple data files where data is grouped by country.
//
// This example uses Data-Forge.
//
"use strict";
const dataForge = require('data-forge');
const inputFileName = "./data/surveys.csv";
function filterRow (inputRow, country) {
return inputRow.country === country;
}
function transformData (inputDataFrame, country) {
return inputDataFrame.where(inputRow => {
return filterRow(inputRow, country);
});
}
function getCountries (inputDataFrame) {
return inputDataFrame
.getSeries("country")
.distinct();
}
function splitDataByCountry (inputDataFrame) {
return getCountries(inputDataFrame)
.aggregate(Promise.resolve(), (prevPromise, country) => {
return prevPromise.then(() => {
const outputDataFrame = transformData(
inputDataFrame,
country
);
const outputFileName = "./output/by-country/" +
country + ".csv";
console.log(">> " + outputFileName);
return outputDataFrame
.asCSV()
.writeFile(outputFileName);
});
});
}
dataForge.readFile(inputFileName)
.parseCSV()
.then(splitDataByCountry)
.then(() => {
console.log("Done!");
})
.catch(err => {
console.error("Error!");
console.error(err && err.stack || err);
});