-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
42 lines (33 loc) · 874 Bytes
/
util.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
const arvish = require('arvish');
const util = {
formatBytes (bytes, decimals = 2) {
if (bytes == 0) {
return '0 B';
}
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + ' ' + sizes[i];
},
async getPackageStats(packageName) {
if (!packageName) {
throw new Error('Empty name given as argument');
}
try {
const result = await arvish.fetch(
`https://bundlephobia.com/api/size?package=${packageName}`,
{
headers: {'User-Agent': 'arvis-bundlephobia'},
maxAge: 60000
}
);
if (result.error) {
throw new Error(result.error);
}
return result;
} catch (e) {
throw new Error(`No result found for ${ packageName }`);
}
}
}
module.exports = util;