-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnormalize.js
More file actions
129 lines (107 loc) · 4.44 KB
/
normalize.js
File metadata and controls
129 lines (107 loc) · 4.44 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
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
console.log('Start morjing')
const fs = require('fs'),
{ promisify } = require('util'),
readdirAsync = promisify(fs.readdir),
readFileAsync = promisify(fs.readFile),
path = require('path');
const filterAndSortFiles = (list)=>
list.filter((f)=>f.charAt(0)!=='.')
.map((f)=>{
return {name: f, id: parseInt(f.split('.')[0], 10)};
})
.sort((a, b)=>a.id - b.id );
const readFiles = async function(dir, parse){
const list = filterAndSortFiles(await readdirAsync(dir));
await Promise.all(
list.map((item)=>{
return readFileAsync(path.join(dir, item.name), 'utf-8')
.then((data)=>{
try{
item.data = parse ? JSON.parse( data ) : data;
}catch(e){
item.data = {}
}
});
}));
return list;
};
const pad = (count, symbol) => count > 0 ? new Array(count+1).join(symbol||' '):'';
(async ()=> {
const paths = {
diffs: path.join( __dirname, 'diffs' ),
pulls: path.join( __dirname, 'pulls' )
};
const pullsList = await readFiles( paths.pulls, true );
const diffsList = await readFiles( paths.diffs, false );
const diffsHash = {};
diffsList.forEach((fileInfo)=>diffsHash[fileInfo.id] = fileInfo);
for( let i = 0, _i = pullsList.length; i< _i; i++ ){
const data = pullsList[i].data;
for( let j = 0, _j = data.length; j < _j; j++ ){
const pull = data[ j ];
const id = pull.number;
if(!(id in diffsHash)){
}else{
diffsHash[id].date = new Date(pull.created_at);
}
}
}
const hash = diffsList.map(function(diff, n){
const added = diff.data.match(/\n\+[^+](.+)/g)
if(added === null)
return []
const rows = added.map(l=>l.replace(/^[\+\n]*/,'').trim());
const matched = rows
.filter(line=>line.substr(1).indexOf('|')>1)
.filter(function(line) {
const tokens = line.split('|');
if(tokens[1].replace(/[^a-zA-Zа-яА-Я]/g, '').trim().length===0) return false;
if(tokens[1].match(/Фамилия/))
return false;
return true;
})
.map(line=>
line
.split('|')
.map((token)=>token.trim())
.filter(token=>token.length>0)
)
.filter(t=>t.length===2);
if(matched.length===0 && n > 4)
console.log(rows, diff)
return matched.map(match=>{return {human:match, diff}})
})
.flat()
.reduce((peopleHash, match)=>{
const human = match.human
if(human[0] in peopleHash && peopleHash[human[0]].job !== human[1])
console.warn(peopleHash[human[0]].job,'!==', human[1])
peopleHash[human[0]] = {job: human[1], date: match.diff.date};
return peopleHash;
}, {});
let resultArr = [];
for(let name in hash){
let info = hash[name];
resultArr.push({name, job: info.job, added: info.date })
}
resultArr.sort((a,b)=>a.added-b.added)
fs.writeFileSync('everybody.json', JSON.stringify(resultArr.map(human=>({name: human.name, job:human.job}))))
resultArr.unshift({name: 'Фамилия и имя', job: 'Должность, компания'});
const max = {name:0, job: 0};
for( let i = 0, _i = resultArr.length; i < _i; i++ ){
const human = resultArr[ i ];
max.name = Math.max(max.name, human.name.length);
max.job = Math.max(max.job, human.job.length);
}
console.log('People counter: '+resultArr.length);
const humanCounter = (resultArr.length+'').length;
const NUMERATE = true;
fs.writeFileSync('out.md',
resultArr.map(
(human, n)=>
'| '+ (NUMERATE?(n>0?n+pad(humanCounter-((n+'').length)):pad(humanCounter)) +' |':'')+
human.name + pad(max.name - human.name.length)+ ' | '+
human.job + pad(max.job - human.job.length)+
' |' + (n===0?'\n|:'+(NUMERATE?pad(humanCounter,'-')+'-|:':'-')+pad(max.name,'-')+'|:'+pad(max.job,'-')+'-|' :'') ).join('\n'));
console.log('Output in out.md');
})();