-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources-largest.js
40 lines (34 loc) · 1.12 KB
/
resources-largest.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
const request = require('request');
const uri = '';
const largestNum = 30;
const getResources = (callback) => {
request.get({ uri }, callback);
}
const totalAttachmentSize = (attachments) => {
if (!attachments) {
return -1;
}
return Object.keys(attachments).reduce((total, key) => {
return total + attachments[key].length
}, 0);
}
const logResults = (largest) => {
largest.forEach((item, index) => {
console.log('Resource ' + (index + 1), '\n========\n', item.doc.title, '\n', item.attachments);
});
}
const findLargest = (err, response) => {
const largest = JSON.parse(response.body).rows.reduce((largest, item, index, arr) => {
console.log('Reading doc ' + index + ' of ' + arr.length);
const doc = item.doc;
const size = totalAttachmentSize(doc._attachments);
if (size > largest[0].size) {
largest.push({ size, doc, attachments: doc._attachments });
largest = largest.sort((a, b) => a.size - b.size);
return largest.length > largestNum ? largest.slice(1) : largest;
}
return largest;
}, [ { size: -1, doc: {} } ]);
logResults(largest);
}
getResources(findLargest);