forked from progedu/adding-up
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
49 lines (44 loc) · 1.47 KB
/
Copy pathapp.js
File metadata and controls
49 lines (44 loc) · 1.47 KB
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
'use strict';
const fs = require('fs');
const readline = require('readline');
const rs = fs.ReadStream('./popu-pref.csv');
const rl = readline.createInterface({ 'input': rs, 'output': {} });
const map = new Map(); // key: 都道府県, value: 集計データのオブジェクト
rl.on('line', (lineString) => {
const columns = lineString.split(',');
const year = parseInt(columns[0]);
const prefecture = columns[2];
const popu = parseInt(columns[7]);
if (year === 2010 || year === 2015) {
let value = map.get(prefecture) || {
popu10: 0,
popu15: 0,
change: null
};
if (year === 2010) {
value.popu10 += popu;
} else if (year === 2015) {
value.popu15 += popu;
}
map.set(prefecture, value);
}
});
rl.on('close', () => {
for (let pair of map) {
const value = pair[1];
value.change = value.popu15 / value.popu10;
}
const rankingArray = Array.from(map).sort(reverse(comparingInt(pair => pair[1].change)));
const rakingStrings = rankingArray.map(
pair => `${pair[0]}: ${pair[1].popu10}=>${pair[1].popu15} 変化率: ${pair[1].change}`);
console.log(rakingStrings);
});
rl.resume();
const reverse = comparator => (source1, source2) => {
return -1 * comparator(source1, source2);
};
const comparingInt = toIntFunction => {
return (source1, source2) => {
return toIntFunction(source1) - toIntFunction(source2);
};
};