forked from dominictarr/npmtop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
48 lines (42 loc) · 1.32 KB
/
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
var request = require('./request');
module.exports = function (cb) {
getScores(function (err, scores) {
if (err) return cb(err);
var total = Object.keys(scores).reduce(function (sum, name) {
return sum + scores[name];
}, 0);
var sorted = Object.keys(scores)
.sort(function (a,b) {
return (scores[b] - scores[a])
|| (a.toLowerCase() < b.toLowerCase() ? -1 : 1)
;
})
.map(function (name, ix) {
return {
rank : ix + 1,
percent : 100 * scores[name] / total,
packages : scores[name],
author : name,
};
})
;
cb(null, sorted);
});
};
function getScores (cb) {
var opts = {
host: 'isaacs.iriscouch.com',
path: '/registry/_design/app/_view/npmTop?group_level=1',
rejectUnauthorized: false,
method: 'GET'
}
request(opts, function (err, buf) {
if (err) return cb(err);
var data = JSON.parse(buf.toString());
var scores = data.rows.reduce(function (acc, row) {
acc[row.key] = row.value;
return acc;
}, {});
cb(null, scores);
});
}