This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from b1f6c1c4/gh-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
h-index.js
100 lines (95 loc) · 2.78 KB
/
h-index.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
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
const _ = require('lodash');
const hindex = require('h-index');
const ss = require('simple-statistics');
const Table = require('cli-table3');
class HIndex {
constructor(all) {
this.all = all;
}
run(repos) {
if (!repos.length) return { ...hindex([]), repoCount: 0, maxRepo: undefined, maxStar: 0 };
if (!this.all) return hindex(repos.map((r) => r.stargazers_count));
const rs = repos.sort((a, b) => b.stargazers_count - a.stargazers_count);
const h = hindex(rs.map((r) => r.stargazers_count));
return {
...h,
repoCount: repos.length,
maxRepo: rs[0].html_url,
maxStar: rs[0].stargazers_count,
};
}
format(oos) {
const root = oos[0];
const os = _.sortBy(oos, ['result.h', 'result.g', 'result.i10', 'result.sum', 'result.maxStar']).reverse();
const table = new Table({
head: [
'Friend of',
'Relation',
'Username',
'h-index',
...(this.all ? [
'g-index',
'i10-index',
'Total Repos',
'Total Stars',
'Most Stars',
'Most-Star Repo',
] : []),
],
chars: {
mid: '',
'left-mid': '',
'mid-mid': '',
'right-mid': '',
},
});
const pu = ({ username, of, relation, result: { sum, h, i10, g, repoCount, maxRepo, maxStar } }) => {
table.push([
of,
relation,
username,
h,
...(this.all ? [
g,
i10,
repoCount,
sum,
maxStar,
maxRepo,
] : []),
]);
};
os.forEach(pu);
if (os.length > 1) {
table.push([undefined, undefined, undefined, ...this.makeSummary(() => undefined, os)]);
table.push([undefined, undefined, '(Sum.)', ...this.makeSummary(ss.sum, os)]);
table.push([undefined, undefined, '(Max.)', ...this.makeSummary(ss.max, os)]);
table.push([undefined, undefined, '(Q3. )', ...this.makeSummary((l) => ss.quantile(l, 0.75), os)]);
table.push([undefined, undefined, '(Med.)', ...this.makeSummary(ss.median, os)]);
table.push([undefined, undefined, '(Q1. )', ...this.makeSummary((l) => ss.quantile(l, 0.25), os)]);
table.push([undefined, undefined, '(Min.)', ...this.makeSummary(ss.min, os)]);
table.push([undefined, undefined, '(Avg.)', ...this.makeSummary(ss.mean, os)]);
pu(root);
}
return table.toString();
}
makeSummary(fun, os) {
const f = (field) => {
const r = fun(os.map((o) => o.result[field]));
if (r === undefined) return r;
return Math.round(r * 100) / 100;
};
return [
f('h'),
...(this.all ? [
f('g'),
f('i10'),
f('repoCount'),
f('sum'),
f('maxStar'),
undefined,
] : []),
];
}
}
module.exports = HIndex;