-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
134 lines (115 loc) · 4.63 KB
/
index.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import _ = require('lodash');
import program = require('commander');
import csv = require('csv-parser');
import fs = require('fs');
import readline = require('readline');
import Tendencies = require('./calculators/Tendencies');
import Proportions = require('./calculators/Proportions');
import EnumFilter = require('./filters/EnumFilter');
import brand = require('./carProperties/brand');
import color = require('./carProperties/color');
import co2EnergyClass = require('./carProperties/co2EnergyClass');
import co2Value = require('./carProperties/co2Value');
import electricHybrid = require('./carProperties/electricHybrid');
import length = require('./carProperties/length');
import powerSource = require('./carProperties/powerSource');
import transmission = require('./carProperties/transmission');
import vehicleClass = require('./carProperties/vehicleClass');
import width = require('./carProperties/width');
import ProportionNormalizer = require('./normalizers/ProportionNormalizer');
let count = 0;
const progressLabel = 'Vehicles processed: ';
const progressIndex = progressLabel.length;
function printCount(recordCount: number): void {
process.stdout.write(recordCount.toString());
}
function printProgress(recordCount: number): void {
readline.cursorTo(process.stdout, progressIndex);
printCount(recordCount);
}
function passesFilters(record: CsvRecord, propertyFilters: PropertyFilter[]): boolean {
return _.every(propertyFilters, (propertyFilter) =>
propertyFilter.filter.filter(record, propertyFilter.property, propertyFilter.acceptedValues)
);
}
function getPercentileForValue(property: CarProperty, options: CommandLineOptions): number | undefined {
return Number(options[property.name]) || undefined;
}
function toCalculation(property: CarProperty, options: CommandLineOptions): PropertyCalculation {
return {
...property,
calculator: property.type === 'Proportions' ? new Proportions(property) : new Tendencies(property),
percentileForValue: getPercentileForValue(property, options),
normalizer: property.type === 'Proportions' && property.normalizerMappings ? new ProportionNormalizer() : undefined,
};
}
function processData(
filename: string,
filters: PropertyFilter[],
properties: CarProperty[],
options: CommandLineOptions
): void {
const calculations = properties.map((property) => toCalculation(property, options));
const printProgressUpdates = !!process.stdout.isTTY;
fs.createReadStream(filename, { encoding: 'latin1' })
.pipe(csv({ separator: ';' }))
.on('data', (record) => {
count += 1;
if (passesFilters(record, filters)) {
calculations.forEach((calculation) => calculation.calculator.processRecord(record[calculation.columnName]));
}
if (printProgressUpdates) {
printProgress(count);
}
})
.on('end', () => {
if (!printProgressUpdates) {
printCount(count);
}
process.stdout.write('\n');
calculations.forEach((calculation) =>
calculation.calculator.processResults(options.language, calculation.percentileForValue, calculation.normalizer)
);
});
}
const filters: PropertyFilter[] = [
{ filter: new EnumFilter(), property: vehicleClass, acceptedValues: [vehicleClass.filterValues.car] },
];
const properties: CarProperty[] = [
color,
length,
width,
co2Value,
co2EnergyClass,
powerSource,
brand,
transmission,
electricHybrid,
];
function validateOptions(cmd: CommandLineOptions): CommandLineOptions {
const { co2: co2Option, language, length: lengthOption, width: widthOption } = cmd;
[co2Option, lengthOption, widthOption].forEach((numberOption) => {
const numberValue = Number(numberOption);
if (numberValue <= 0) {
console.error(`error: invalid numeric value '${numberOption}', must be a positive number`);
process.exit(1);
}
});
if (!_.includes(['fi', 'sv', 'en'], language)) {
console.error(`error: invalid language value '${language}', must be one of fi|sv|en`);
process.exit(1);
}
return cmd;
}
program
.command('process <filename>')
.option('-c, --CO2 <CO2 emissions in g/km>', 'show percentile for given CO2 emissions when compared to other cars')
.option('-e, --length <length in mm>', 'show percentile for given length when compared to other cars')
.option('-w, --width <width in mm>', 'show percentile for given width when compared to other cars')
.option('-l, --language <language code>', 'language in which to show info labels: fi|sv|en', 'fi')
.action((filename, cmd) => {
const options = validateOptions(cmd);
process.stdout.write(progressLabel);
processData(filename, filters, properties, options);
});
program.parse(process.argv);